Docker installation on GCP
We are going to learn the way to set up and install docker on google compute engine.
All source codes and explanations came from How To Install and Use Docker on Ubuntu 16.06 on DigitalOcean
-
Docker installation on GCP
- Step 1. Create a new Google Compute Engine (GCE) instance
- Step 2. Install Docker on your new Google Compute Engine Machine
- Step 3. Executing the Docker Command Without Sudo (Optional)
- Step 4. Working with Docker Images
- Step 5. Running a Docker Container
- Step 6. Managing Docker Containers
- Step 7. Committing Changes in a Container to a Docker Image
- Step 8. Pushing Docker Images to a Docker Repository
Step 1. Create a new Google Compute Engine (GCE) instance
Go to your Google Cloud Platform console
-
Create a new Google Compute Engine, with Debian 8 or Linux distort of your choice.
Give it a name
Choose a zone
Choose a machine type
Allow HTTP traffic
Click “Create”
SSH into your new Google Compute Engine
Step 2. Install Docker on your new Google Compute Engine Machine
-
Login as Super user
sudo -s
First, in order to ensure the downloads are valid, 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 $(lsb_release -cs) stable"
Next, update the package database with the Docker packages from the newly added repo:
sudo apt-get update
docker-ce:
Installed: (none)
Candidate: 5:18.09.0~3-0~ubuntu-xenial
Version table:
5:18.09.0~3-0~ubuntu-xenial 500
500 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
18.06.1~ce~3-0~ubuntu 500
500 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
18.06.0~ce~3-0~ubuntu 500
500 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
18.03.1~ce-0~ubuntu 500
500 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
18.03.0~ce-0~ubuntu 500
500 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
17.12.1~ce-0~ubuntu 500
500 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
17.12.0~ce-0~ubuntu 500
500 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
17.09.1~ce-0~ubuntu 500
500 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
17.09.0~ce-0~ubuntu 500
500 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
17.06.2~ce-0~ubuntu 500
500 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
17.06.1~ce-0~ubuntu 500
500 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
17.06.0~ce-0~ubuntu 500
500 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
17.03.3~ce-0~ubuntu-xenial 500
500 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
17.03.2~ce-0~ubuntu-xenial 500
500 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
17.03.1~ce-0~ubuntu-xenial 500
500 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
17.03.0~ce-0~ubuntu-xenial 500
500 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
Finally, install Docker:
sudo apt-get install -y docker-ce
Docker should now be installed, the daemon started, and the process enabled to start on boot. Check that it’s running:
sudo systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2018-12-27 15:50:28 UTC; 39s ago
Docs: https://docs.docker.com
Main PID: 14035 (dockerd)
CGroup: /system.slice/docker.service
└─14035 /usr/bin/dockerd -H unix://
Dec 27 15:50:27 database-server dockerd[14035]: time=”2018-12-27T15:50:27.226811162Z” level=warning msg=”Your kernel does not support swap memory limit”
Dec 27 15:50:27 database-server dockerd[14035]: time=”2018-12-27T15:50:27.227267680Z” level=warning msg=”Your kernel does not support cgroup rt period”
Dec 27 15:50:27 database-server dockerd[14035]: time=”2018-12-27T15:50:27.227457038Z” level=warning msg=”Your kernel does not support cgroup rt runtime”
Dec 27 15:50:27 database-server dockerd[14035]: time=”2018-12-27T15:50:27.229318131Z” level=info msg=”Loading containers: start.”
Dec 27 15:50:27 database-server dockerd[14035]: time=”2018-12-27T15:50:27.712759962Z” level=info msg=”Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. Daemon opti
Dec 27 15:50:27 database-server dockerd[14035]: time=”2018-12-27T15:50:27.796363894Z” level=info msg=”Loading containers: done.”
Dec 27 15:50:27 database-server dockerd[14035]: time=”2018-12-27T15:50:27.840844599Z” level=info msg=”Docker daemon” commit=4d60db4 graphdriver(s)=overlay2 version=18.09.0
Dec 27 15:50:27 database-server dockerd[14035]: time=”2018-12-27T15:50:27.841562244Z” level=info msg=”Daemon has completed initialization”
Dec 27 15:50:28 database-server systemd1: Started Docker Application Container Engine.
Dec 27 15:50:28 database-server dockerd[14035]: time=”2018-12-27T15:50:28.050783220Z” level=info msg=”API listen on /var/run/docker.sock”
Step 3. Executing the Docker Command Without Sudo (Optional)
If you want to avoid typing sudo whenever you run the docker command, add your username to the docker group:
sudo groupadd docker
sudo usermod -aG docker ${USER}
To apply the new group membership, you can log out of the server and back in, or you can type the following:
sudo passwd root
su - ${USER}
You will be prompted to enter your user’s password to continue. Afterwards, you can confirm that your user is now added to the docker group by typing:
id -nG
Step 4. Working with Docker Images
docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.To generate this message, Docker took the following steps:
The Docker client contacted the Docker daemon.
The Docker daemon pulled the “hello-world” image from the Docker Hub.
(amd64)The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.To try something more ambitious, you can run an Ubuntu container with:
docker run -it ubuntu bash
output
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
32802c0cfa4d: Pull complete
da1315cffa03: Pull complete
fa83472a3562: Pull complete
f85999a86bef: Pull complete
Digest: sha256:6d0e0c26489e33f5a6f0020edface2727db9489744ecc9b4f50c7fa671f23c49
Status: Downloaded newer image for ubuntu:latest
root@ffc2620c415b:/# exit
exit
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/For more examples and ideas, visit:
https://docs.docker.com/get-started/
docker images
output
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 93fd78260bd1 5 weeks ago 86.2MB
hello-world latest 4ab4c602aa5e 3 months ago 1.84kB
Step 5. Running a Docker Container
Containers can be much more useful than that, and they can be interactive. After all, they are similar to virtual machines, only more resource-friendly.
As an example, let’s 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
output
root@f39fe297ef16:/# apt-get update
Get:1 http://security.ubuntu.com/ubuntu bionic-security InRelease [83.2 kB]
Get:2 http://archive.ubuntu.com/ubuntu bionic InRelease [242 kB]
Get:3 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [135 kB]
Get:4 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:5 http://archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
Get:6 http://security.ubuntu.com/ubuntu bionic-security/multiverse amd64 Packages [1367 B]
Get:7 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [300 kB]
Get:8 http://archive.ubuntu.com/ubuntu bionic/universe amd64 Packages [11.3 MB]
Get:9 http://archive.ubuntu.com/ubuntu bionic/restricted amd64 Packages [13.5 kB]
Get:10 http://archive.ubuntu.com/ubuntu bionic/main amd64 Packages [1344 kB]
Get:11 http://archive.ubuntu.com/ubuntu bionic/multiverse amd64 Packages [186 kB]
Get:12 http://archive.ubuntu.com/ubuntu bionic-updates/multiverse amd64 Packages [6931 B]
Get:13 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [900 kB]
Get:14 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [618 kB]
Get:15 http://archive.ubuntu.com/ubuntu bionic-updates/restricted amd64 Packages [10.7 kB]
Get:16 http://archive.ubuntu.com/ubuntu bionic-backports/universe amd64 Packages [3655 B]
Fetched 15.3 MB in 3s (5453 kB/s)
Reading package lists... Done
Then install any application in it. Let’s install Node.js:
apt-get install -y nodejs
This installs Node.js in the container from the official Ubuntu repository. When the installation finishes, verify that Node.js is installed:
node -v
Output
v8.10.0
Step 6. Managing Docker Containers
After using Docker for a while, you’ll have many active (running) and inactive containers on your computer. To view the active ones, use:
docker ps
You will see output similar to the following:
output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
To view all containers — active and inactive — run docker ps with the -a switch:
docker ps -a
output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f39fe297ef16 ubuntu "/bin/bash" 9 hours ago Exited (0) 8 hours ago thirsty_turing
c5451bf09731 ubuntu "/bin/bash" 9 hours ago Exited (0) 9 hours ago nostalgic_archimedes
ffc2620c415b ubuntu "bash" 9 hours ago Exited (0) 9 hours ago unruffled_wright
To view the latest container you created, pass it the -l switch:
docker ps -l
output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f39fe297ef16 ubuntu "/bin/bash" 9 hours ago Exited (0) 8 hours ago thirsty_turing
To start a stopped container, use docker start, followed by the container ID or the container’s name. Let’s start the Ubuntu-based container with the ID of :
docker start f39fe297ef16
To stop a running container, use docker stop, followed by the container ID or name. This time, we’ll use the name that Docker assigned the container, which is thirsty_turing.
docker stop thirsty_turing
Once you’ve decided you no longer need a container anymore, remove it with the docker rm command, again using either the container ID or the name. Use the docker ps -a command to find the container ID or name for the container associated with the hello-world image and remove it.
docker rm thirsty_turing
Step 7. Committing Changes in a Container to a Docker Image
This section shows you how to save the state of a container as a new Docker image.
docker commit -m "What did you do to the image" -a "Author Name" container-id repository/new_image_name
The -m switch is for the commit message that helps you and others know what changes you made, while -a is used to specify the author. The container ID is the one you noted earlier in the tutorial when you started the interactive Docker session. Unless you created additional repositories on Docker Hub, the repository is usually your Docker Hub username.
For example, for the user j2hoon85, with the container ID of a2cd48b54e5c, the command would be:
docker commit -m "added node.js" -a "j2hoon85" a2cd48b54e5c j2hoon85/ubuntu-nodejs
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
j2hoon85/ubuntu-nodejs latest b318da5e1778 9 seconds ago 170MB
ubuntu latest 93fd78260bd1 5 weeks ago 86.2MB
hello-world latest 4ab4c602aa5e 3 months ago 1.84kB
In the above example, ubuntu-nodejs is the new image, which was derived from the existing ubuntu image from Docker Hub. The size difference reflects the changes that were made. In this example, the change was that Node.js was installed. Next time you need to run a container using Ubuntu with Node.js pre-installed, you can just use the new image.
Step 8. Pushing Docker Images to a Docker Repository
To push an image to Docker Hub or any other Docker registry, you must have an account there.
This section shows you how to push a Docker image to Docker Hub. To learn how to create your own private Docker registry, check out How To Set Up a Private Docker Registry on Ubuntu 14.04.
docker login -u j2hoon85
You’ll be prompted to authenticate using your Docker Hub password. If you specified the correct password, authentication should succeed.
Note: If your Docker registry username is different from the local username you used to create the image, you will have to tag your image with your registry username. For the example given in the last step, you would type:
docker tag j2hoon85/ubuntu-nodejs j2hoon85/ubuntu-nodejs
Then you can push your own image using:
docker push j2hoon85/ubuntu-nodejs
To push the ubuntu-nodejs image to the sammy repository, the command would be:
docker push j2hoon85/ubuntu-nodejs
The push refers to repository [docker.io/j2hoon85/ubuntu-nodejs]
7159bda7f8e1: Pushed
b9b7103af585: Mounted from library/ubuntu
ca2991e4676c: Mounted from library/ubuntu
a768c3f3878e: Mounted from library/ubuntu
bc7f4b25d0ae: Mounted from library/ubuntu
latest: digest: sha256:a53344a922590465bd75afd1419c732392796392f25642eb1ef58b9dfbec05f8 size: 1362
The process may take some time to complete as it uploads the images, but when completed, the output will look like this: