Docker Cheatsheet

Docker Cheatsheet


This post is a cheat sheet for Docker. I wrote this for myself so if I ever have trouble remembering some of the more common commands I can just look them up on my own site. And maybe someone out there on the internet will find this useful for themselves as well. 

Note: After learning docker you might also want to learn about container orchestration tools like Kubernetes that will manage deployment and load balancing for you.  

Installing Docker

You can download and install Docker from here:
For Linux you can also do a very simple install via command line by using this script which you can get with curl like so: curl -fsSL -o and then run like so: sudo sh


In this example, I will make a simple web server to demonstrate how to use docker.
Make a new folder and add a new file named just dockerfile (no extensions). I’ll also create a folder called src (you can name it differently) to put the webpage files in.
Now let’s make a simple dockerfile “script” that will get an image with Ubuntu, install an apache web server and copy our webpage files to the server directory.
#Select base image to start from. (base image):(version) 
FROM ubuntu:latest 

#Execute command in the commad line inside the image. 
RUN apt-get -y update
RUN apt-get install -y apache2

#Note that a service will be running on port 80.

#WORKDIR sets the current directory kind of like cd.
WORKDIR /var/www/html
#Copies file into the specified directory inside the container.
COPY ./src/index.html /var/www/html/index.html

#Specifies what should be run when the container starts up.
ENTRYPOINT ["/usr/sbin/apache2ctl"]
#CMD adds a command to the CLI when the docker image is started up.
#Example: docker run "yourImageName" -D "FOREGROUND"
This is how it should all look like at the end:
Now go to the command line(make sure you are in the folder where the docker file is) and run this command: docker build -t test-build . to make an image.
You can run this command: docker images to see the newly created images or you can just open the Docker Desktop application and select the Images tab.
Next, you can run this command: docker run -d -p 80:8080 test-build to start up a new docker container from the image or you can just click the run button in Docker Desktop.
And the final result …

Docker CLI Commands

Here is a list of some common docker commands with short explanations.
//Build docker container. 
//docker, command,  tag,  local docker dir 
docker build -t test-build \. 
//Run the container. 
//docker, command,  -it(interactive terminal), port external map to internal, image name 
docker run -it -p 80:8080 test-build 
//Run the container in the background.
//docker, command,  -d(detached), port external map to internal, image name 
docker run -d -p 80:8080 test-build 
//Show running docker containers. 
docker ps
//Stop container. 
//docker, command, container name 
docker stop TestContainer 
//Show image builds. 
//docker, command 
docker images 
//Get bash access to container. 
//docker, command, it(interactive terminal), container ID, path to bash 
docker run -it 5sd4f564s6df /bin/bash 
//Or if the above doesn’t work:
docker run -it 5sd4f564s6df /bin/sh 
//Removing images and containers.
docker rm  //remove container 
docker rmi //remove image

//Prompts you for your credentials.
docker login

//Pulls an image from docker hub or a private repository.
docker pull nameOfTheImage

//Pushes an image to docker hub or a private repository.
docker push nameOfTheImage
Note: You can make your own private container registry(I once used Azure Container Registry for example) or use a public one like Docker Hub to push/pull your images to/from.

Docker Compose

Docker compose can be used to run multiple containers at once. For example, if you want to set up something like a WordPress site you will need a web server and a MySQL database. We can use the docker-compose.yaml file to define all these services and their behavior.

Note: Ideally you would just get a WordPress container image that has all of this already installed. This example is used just to demonstrate how to use docker compose.

#Specify the docker version we are using.
version: "3"

#Define services/containers.
  #1. - Web server.
    #Specify base image. Could be your image or one from docker hub.
    image: "php:7.4-apache"
    #Connects ./src from project folder to /var/www/html folder within the container. 
      - "./src/wordpress:/var/www/html/wordpress"
    #Restart on crash.
    restart: 'always'
    #Specifies that the DB should be running before the web server.
      - db
    #Maps internal docker ports to outer port.
      - '8080:80'
    #Specifies connection with the DB service.
      - db
    networks: #Connects this container to myNewNetwork network.
      - myNewNetwork  
  #2. - Database.
    #Specify base image.
    image: mysql:latest
    #Restart on crash.
    restart: 'always'
    #When a container is shut down it will lose all it's data.
    #We can define volumes a wher we can store data Persistance 
      - ./src/dbData:/var/lib/mysql
    #Define environmental variables that will be passed to the container when it runs.
    environment: #${ someVariableName } can be used to define a variable you will need to pass with the docker-compose command.
                 #This way whoever uses this can easily set their own user./pass. without changing the docker-compose file.  
      MYSQL_DATABASE: "wordpress"
      MYSQL_USER: "user" #${USERNAME} <== example of how it can be used.
      MYSQL_PASSWORD: "somepassword" #"${MYSQL_PASSWORD}" <== example how it can be used.
      MYSQL_ROOT_PASSWORD: "somepassword" #"${MYSQL_PASSWORD}" <== example how it can be used.
    networks: #Connects this container to myNewNetwork network.
      - myNewNetwork  
#Here we can define our networks.
  myNewNetwork: #Define a new network.
    driver: bridge #Sets bridge mode(similar to when you setup VMs).

Docker Compose Commands

#Builds and runs all the containers/images specified in the docker-compose.yaml file.
docker-compose up

#Stops all the containers/images specified in the docker-compose.yaml file.
docker-compose down


If we go to localhost:8080 after running docker-compose up we should get the WordPress installation wizard.

Leave a Reply

Your email address will not be published. Required fields are marked *

The following GDPR rules must be read and accepted:
This form collects your name, email and content so that we can keep track of the comments placed on the website. For more info check our privacy policy where you will get more info on where, how and why we store your data.

Advertisment ad adsense adlogger