Skip to content

Fethi's Blog

About

Practical Docker Cheatsheet

1 min read

What is Docker?

To put it briefly, Docker is a container technology that you can run an application in an isolated environment. You can find more details about Docker at https://docs.docker.com/get-started/overview/

Installation

1-) Install Docker with snap

1sudo snap install docker

If it is ok when you execute the following command

1sudo docker run hello-world

You will see the following output

1Unable to find image 'hello-world:latest' locally
2latest: Pulling from library/hello-world
3256ab8fe8778: Pull complete
4Digest: sha256:4cf9c47f86df71d48364001ede3a4fcd85ae80ce02ebad74156906caff5378bc
5Status: Downloaded newer image for hello-world:latest
6
7Hello from Docker!
8This message shows that your installation appears to be working correctly.
9
10To generate this message, Docker took the following steps:
11 1. The Docker client contacted the Docker daemon.
12 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
13 (arm64v8)
14 3. The Docker daemon created a new container from that image which runs the
15 executable that produces the output you are currently reading.
16 4. The Docker daemon streamed that output to the Docker client, which sent it
17 to your terminal.
18
19To try something more ambitious, you can run an Ubuntu container with:
20 $ docker run -it ubuntu bash
21
22Share images, automate workflows, and more with a free Docker ID:
23 https://hub.docker.com/
24
25For more examples and ideas, visit:
26 https://docs.docker.com/get-started/

2-) Create a group named docker to be able to use docker commands without sudo

1sudo groupadd docker

3-) Add your user to the docker group

1sudo usermod -aG docker $USER

4-) Reboot your system

1sudo reboot

5-) If everything is ok you can able execute the following command without sudo

1docker run hello-world

Usage examples

Pull and run the latest MongoDB from Docker with the name mongo

1docker run -d -p 27017:27017 -v ~/mongo/data:/data/db --name mongo mongo

With the -v parameter MongoDB data become persistent to host's ~/mongo/data directory. In this way, we don't lose any data when we delete containers

Drop to container's shell

1docker exec -it mongo bash

Fetch the logs of a container

1docker logs -f mongo

Fetch the last 100 logs of a container

1docker logs -f --tail 100 mongo

Stop container

1docker stop mongo

Start container

1docker start mongo

Remove container

1docker rm mongo

List the running containers

1docker ps

List the running and stopped containers

1docker ps -a

List the images

1docker images

Remove image

1docker rmi <image-id>

List the networks

1docker network ls

Remove all running and stopped containers

1docker rm -f $(docker ps -aq)

Pull and run the latest Ubuntu with the name virtualws, drop its shell

1docker run -i -t -v ~/VirtualWorkspace/:/home/VirtualWorkspace/ --name virtualws ubuntu bash

Dockerize a Node.js with following Dockerfile

1FROM node:12-slim
2
3WORKDIR /usr/src/app/
4
5COPY . .
6
7RUN npm install
8
9CMD [ "node", "index.js"]

Build an image with the name nodejs

1docker build -t nodejs .

Run container in the background

1docker run -d -p 8080:8080 nodejs

Resources

© 2020 by Fethi E. UZUN. All rights reserved.
Based on Gatsby Minimal Blog Theme