Docker

So. What is Docker?

Docker is a tool that allows you to run applications in a containerized environment. It is a great tool for developers and sysadmins as it allows you to run applications in a sandboxed environment that is isolated from the host system. This allows you to run multiple applications in the same system without having to worry about conflicts between them.

Docker allows you to run an application in something called a “Container” which is an environment that is isolated from the host system. It is especially useful in cases where you need to have libraries or packages with different versions in the same system.

How does it work?

Docker is started by creating a Dockerfile which is a file that contains the instructions for building the container. The Dockerfile is then used to build the container which is then run by the Docker daemon. (The Docker daemon is a service that runs in the background and is responsible for running the containers.)

# syntax=docker/dockerfile:1
FROM node:12-alpine
RUN apk add --no-cache python2 g++ make
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]
EXPOSE 3000

Instead of a Dockerfile, you are able to also use a Docker image which is a pre-built container that you can use. Each image is made for specific purposes and is usually made by the community at Docker Hub.

Docker Hub

When a container is run using docker run it is then given a name and a unique ID. The name is used to identify the container and the ID is used to identify the container in the Docker daemon. By also using the -d flag, the container is run in the background and is not attached to the terminal.

Docker Number and ID

You can look at running containers using docker ps and you can look at all containers using docker ps -a which will show even exited ones.

Docker commands

Here are some of the most common commands that you will use when working with Docker.

docker run

The docker run command is used to run a container. It takes the name of the image that you want to run as an argument. You can also specify the name of the container using the --name flag. If you don’t specify a name Docker will generate a random name for you.

docker run --name my-container my-image

docker ps

The docker ps command is used to list running containers. You can also use the -a flag to list all containers.

docker ps

docker stop

The docker stop command is used to stop a running container. It takes the name of the container as an argument.

docker stop my-container

docker start

The docker start command is used to start a stopped container. It takes the name of the container as an argument.

docker start my-container

docker rm

The docker rm command is used to remove a container. It takes the name of the container as an argument.

docker rm my-container

docker exec

The docker exec command is used to execute a command in a running container. It takes the name of the container as an argument and the command that you want to execute.

docker exec my-container ls

docker build

The docker build command is used to build a container from a Dockerfile. It takes the path to the Dockerfile as an argument.

docker build .

docker pull

The docker pull command is used to pull an image from Docker Hub. It takes the name of the image as an argument.

docker pull my-image

docker push

The docker push command is used to push an image to Docker Hub. It takes the name of the image as an argument.

docker push my-image

Docker is a pretty amazing tool to use and is especially useful when working with multiple applications and I couldn’t recommend it enough.

Here is also an amazing Tutorial by Programming with Mosh that you can watch to learn more about Docker!