This is a step-by-step guide to install Docker on Linux Mint 18.3 Cinnamon 64-bit, including removing previous versions, adding the Docker repository, and creating a Docker user group. It also covers managing Docker daemon auto-start, pulling a PostgreSQL image from the Docker registry, creating and managing containers, validating a PostgreSQL container using pgAdmin, building Docker images for Node.js applications, and optimizing image size using NGINX to host web content.
Introduction
This is a note on Docker.
Background
In this note, I want to address the following topics:
- How to install Docker
- How to pull an image and start a container
- How to build your own image
Install Docker
Docker is easy to install due to its good documentation. But you may encounter some troubles if your environment is slightly different. In this note, I will use a VM running Linux Mint 18.3 Cinnamon 64-bit. If you want to repeat my steps, you can refer to my earlier note to create your own VM. Of course, you are free to try any other operating systems.
Remove Previous Version of Docker
Because the Docker installation packages have some broken changes, Docker recommends to check if the previous version of Docker is installed and remote it.
sudo apt-get remove docker docker-engine docker.io
Add Docker Repository
The most recent versions of Docker are not available in the Linux Ubuntu/Mint repositories. You need to add the Docker repository.
sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo apt-key fingerprint 0EBFCD88
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable"
Available Docker Versions
You can check the available Docker versions by the following command:
apt-cache policy docker-ce
Install Docker
After adding the Docker repository and deciding on your desired Docker version, you can install Docker on your computer.
sudo apt-get update
sudo apt-get install docker-ce=18.06.1~ce~3-0~ubuntu
In this note, I installed the version - "18.06.1~ce~3-0~ubuntu". With this command, we actually installed three packages.
docker-ce
docker-ce-cli
containerd.io
If we want to remove docker, we may consider removing all of them.
Create a Docker User Group
By default, Docker runs with sudo
privilege. If you do not want to type sudo
all the time, Docker recommends to create a "docker" user group and add yourself to it.
sudo groupadd docker
sudo usermod -aG docker $USER
getent group docker
You may need to restart your computer to let the docker
user group to take effect.
Fix the Problem of Previous "Sudo"
If you have used Docker with sudo
, you may have a problem to run Docker with your group privilege. You can fix it by removing ~/.docker/ or change the owner and permissions of the ~/.docker/ directory.
sudo chown "$USER":"$USER" /home/"$USER"/.docker -R
sudo chmod g+rwx "/home/$USER/.docker" -R
Disable Auto-start Docker Daemon
When we install Docker, we installed both Docker daemon and Docker CLI. The Docker Daemon is required by the Docker CLI to function. It is configured to auto-start when your computer starts. If your computer is a development computer, you may not always want it to auto-start. You can choose to disable it.
sudo systemctl disable docker.socket
sudo systemctl disable docker.service
You can manually start the Docker daemon when you need it by the following commands:
sudo systemctl start docker.socket
sudo systemctl start docker.service
You can check if the Docker daemon is running by the following commands:
systemctl is-active docker.service
systemctl is-active docker.socket
Now we have successfully installed Docker, we can run the simplest Docker command to check its version.
docker -v
Docker Registry & Image & Container
In this section, I will pull a Docker postgres image from the Docker registry and start a container instance from it. The goal of this section is to understand the following:
- What is a Docker registry
- What is a Docker image and how to pull a Docker image from the registry
- What is a Docker container and how to start a container from a Docker image
The Docker Registry
A Docker registry is a stateless server side application that stores the distribute Docker images. You can find the default registry by the following command:
docker info | grep Registry
The default registry is a publicly available service where we can pull the postgre image from.
The Docker Image
A Docker image is the base of containers. An image is an ordered collection of root filesystem changes and the corresponding execution parameters for use within a container runtime. An image typically contains a union of layered filesystems stacked on top of each other. An image does not have state and it never changes. You can pull the postgre image from the default Docker registry by the following command:
docker pull postgres:10.5
With this command, we have pulled the image tagged as 10.5
. You can find the image by the following command:
docker image ls
If you have not started any containers from the docker image and if you want to remove the image, you can use the following command:
docker image rm postgres:10.5
The Docker Container
A Docker image does not run by itself. It serves as the template for the Docker containers. You can create a container by the following command:
docker run --name ps-instance-1 -e POSTGRES_PASSWORD=password -d -p 5432:5432 postgres:10.5
By this command, you have created and started a postgres docker container with the following attributes:
- The container is named as
ps-instance-1
- The container instance has a default super user
postgres
and its password is password
- The port number that it listens to is
5432
You can find the container by the following command:
docker container ls
docker container ls -a
The -a
option lists all the containers including the ones that are not started. You can stop and start a container by the following command:
docker container stop ps-instance-1
docker container start ps-instance-1
If you like, you can remove a container by the following command:
docker container rm ps-instance-1
Validate the Postgres Container
In order to validate the Postgres container, I choose to install the pgAdmin. In an Ubuntu/Mint computer, you can use the following commands to add the pgAdmin
repository:
sudo apt-get install wget ca-certificates
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo sh -c \
'echo "deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main" > \
/etc/apt/sources.list.d/pgdg.list'
sudo apt-get update
apt-cache policy pgadmin4
You can then install the pgAdmin
by the following command:
sudo apt-get install pgadmin4=3.4-1.pgdg16.04+1
You can then start the pgAdmin
and connect to the Postgres instance:
You can use the super user postgres
and password
to connect to the Postgres container instance. If everything goes well, you should find that the Postgres instance is running fine.
Build Your Docker Image
We have been able to install Docker and have pulled an existing Docker image and started a container from it. Now it is the time to build a Docker image.
The attached is a Node application that I created earlier that has a Redux example. If you are interested, you can take a look at my earlier note. To build a Docker image, we need to create a Dockerfile
.
FROM node:8.12.0-alpine
WORKDIR /app/node
COPY package.json .
RUN npm install --production
COPY app.js .
COPY client/index.html client/index.html
COPY client/build/ client/build/
EXPOSE 3000
CMD ["node", "app.js"]
- We need to select a base Docker image that has Node pre-installed;
- We need to choose a working directory;
- We need to copy the package.json file to the image and install the node_modules;
- We need to copy all the files necessary for the application to run;
- We need to expose the port number that the application listens;
- We need to provide the
CMD
that Docker can start a container.
Because the example Node application used "Webpack" to bundle the client-side code, we need to build it first. We can issue the following command to install the node_modules
.
npm install
You can then issue the following command to bundle the client-side code. The bundle will be located in the client/build/ directory.
npm run pack-p
After the bundle, you can issue the following command to build the Docker image.
docker image build -t "redux-example:0.0.1" .
You can start a container on this image by the following command:
docker run --name "redux-example-1" -p 3000:3000 -d redux-example:0.0.1
If everything goes well, you should be able to start the container redux-example-1
. You can then go to "http://localhost:3000/" in your browser.
If you like, you can remove the container by the following command:
docker container rm -f redux-example-1
You can also remove the image that you just built by the following command:
docker image rm redux-example:0.0.1
Build Docker Image by Nginx
While it is not difficult to build the docker image, it is always nice to have a smaller image. With the big node_module
, it is difficult to reduce the size of the image. In fact, we can host the React application by NGNIX. In order to do it, we can create another Dockerfile named Dockerfile.nginx.
FROM nginx:1.13.9-alpine
COPY ./client/index.html /usr/share/nginx/html
COPY ./client/build/ /usr/share/nginx/html/build
EXPOSE 80
VOLUME /home/song/sandbox/redux-example/redux-example/client:/usr/share/nginx/html
CMD ["nginx", "-g", "daemon off;"]
To build the image that uses NGINX to host the web contents, we can issue the following command.
docker image build -t "redux-example:nginx" -f Dockerfile.nginx .
We can see that the image is merely 18.3MB.
We can then start the container by the following command:
docker run --name "redux-example-nginx" -p 80:80 -d redux-example:nginx
If we go to the URL "http://localhost/", we should see that the container is running nicely. We can also attach a volume when you start the container.
HOST_PATH=/home/song/sandbox/redux-example/redux-example/client
CONTAINER_PATH=/usr/share/nginx/html
docker run --name "redux-example-nginx" -p 80:80 -d -v $HOST_PATH:$CONTAINER_PATH redux-example:nginx
If you now make any changes to your React application in the host computer and if you reload the web page, you will find that the content is refreshed without rebuilding the image.
Points of Interest
- This is a note on Docker.
- I hope you like my poss and I hope this note can help you in one way or the other.
History
- 19th October, 2018: First revision