* Introduction Docker
Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.
* Install Docker on Ubuntu/Linux Mintfont-family
Prerequisites:
- One Ubuntu 18.04 or Linux Mint 18.3 server set up by following the Ubuntu 18.04 initial server setup guide, including a sudo non-root user and a firewall.
- An account on Docker Hub if you wish to create your own images and push them to Docker Hub, as shown in Steps 6 and 7
Install:
Step 1 — Installing Docker:
- First, update your existing list of packages
sudo
apt update
- Install a few prerequisite packages
sudo apt install apt-transport-https ca-certificates curl software-properties-common
- Add the GPG key for the official Docker repository to your system
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
- Add the Docker repository to APT sources
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"
- Update the package database with the Docker packages from the newly added repository
sudo apt update
- Make sure you are about to install from the Docker repository instead of the default Ubuntu repository
apt-cache policy docker-ce
You will see output of apt-cache policy docker-ce. The version of Docker maybe different
- Last, install docker 😀
sudo apt install docker-ce
- Check Docker run or not
sudo systemctl status docker
The output show that the service is active and running
* Use Docker
Step 2 — Using the Docker Command
- The syntax for the Docker with options, arguments
docker [option] [command] [arguments]
To view all subcommands
docker
Run ‘docker COMMAND –help’ for more information on a command
docker COMMAND --help
- To view system-wide information about Docker
docker info
Step 3 — Working with Docker Images
- Anyone can host their Docker images on Docker Hub. To check whether you can access and download images from Docker Hub
docker run openjdk
The first time when you get the image
The next time, you run, the result
- You can search for images available on Docker Hub by using the docker command with the search sub-command
docker search ubuntu
- To see the images that have been downloaded to your computer
docker images
Step 4 — Running a Docker Container
- Run a container using the latest image of Ubuntu. The combination of the -i and -t switches gives you interactive shell access into the container
docker run -it ubuntu
Step 5 — Managing Docker Containers
- To view active containers
docker ps
- You can start or stop the container. If start it, use ID of the container. Stop or remove it, use Name of it.
docker stop angry_swanson
docker start d1a3c84df680
docker rm angry_swanson
Step 6 — Committing Changes in a Container to a Docker Image
- Commit image to the docker.
docker commit -m "What you did to the image" -a "habogay" d1a3c84df680 habogay/spring-boot-docker-gcb
- Listing the Docker images
docker images
Step 7 — Pushing Docker Images to a Docker Repository
You need to login the Hub Docker before push the image.
docker login -u habogay
- Push the image to the Hub Docker
docker push habogay/spring-boot-docker-gcb
- Listing the Docker images
docker ps
Demo spring-boot-docker-gcb
Demo project for Spring Boot Docker use Google Cloud Build service.
Step 1:
Clone this repository.
git clone [email protected]:habogay/spring-boot-docker-gcb.git
Step 2:
Go to the spring-boot-docker-gcb folder, and run.
cd spring-boot-docker-gcb
mvn clean package
Step 3:
Build docker: “habogay” is reposity name. “spring-boot-docker-gcb” is image name. “.” is current path.
docker build -t habogay/spring-boot-docker-gcb .
Step 4:
Run docker from local port 9999
docker run -p 9999:8080 habogay/spring-boot-docker-gcb
Step 5:
Check docker works or not on a browser: http://localhost:9999/ . 9999 is the port that you configured.
Push image
Push the image to the Docker Hub.
docker push habogay/spring-boot-docker-gcb
Note:
If you don’t login to Hub Docker yet, please login your hubDocker’s account before push the image: docker login -u [username]
docker login -u habogay
Pull image
Step 1:
Pull the image from the Hub Docker.
docker pull habogay/spring-boot-docker-gcb
Step 2:
Run the image
docker run -p 9999:8080 habogay/spring-boot-docker-gcb
bài viết hữu ích, tôi rất cảm kích.
thanks for your help, i appreciate it.