Posted on: Written by: K-Sato
⚠️ This article was posted over 2 years ago. The information might be outdated. ⚠️

Table of Contents

Great Resources

Great introductory posts about Docker

Official docker documents

Docker Commands

  • docker images – Check the images on your system.

  • docker ps – Check a list of your running containers.

  • docker search <image> – Searching an image in the Docker Hub.

  • docker run – Runs a command in a new container.

  • docker start – Starts one or more stopped containers

  • docker stop – Stops one or more running containers

  • docker build – Builds an image form a Docker file

  • docker pull – Pulls an image or a repository from a registry

  • docker push – Pushes an image or a repository to a registry

  • docker export – Exports a container’s filesystem as a tar archive

  • docker exec – Runs a command in a run-time container

  • docker search – Searches the Docker Hub for images

  • docker attach – Attaches to a running container

  • docker commit – Creates a new image from a container’s changes

  • docker rmi -f $(docker images -q) – Delete all the existing images on your system. (-f is the force option.)

  • docker image rm [OPTIONS] IMAGE [IMAGE...] - Delete specific images.

  • docker prune - Remove unused data

  • A Guide to Docker Commands with Examples

  • docker system prune

  • Docker Base Commands

  • How To Remove Docker Images, Containers, and Volumes

  • How to remove old and unused Docker images - Stack Overflow

Docker Compose commands

  • docker-compose ps — lists all the services in a network.
  • docker-compose build — generates any needed images from custom Dockerfiles. It will not pull images from the Docker hub, only generate custom images.
  • docker-compose up — brings up the network for the services to run in
  • docker-compose stop — stops the network and saves the state of all the services
  • docker-compose start — restarts the services and brings them back up with the state they had when they were stopped
  • docker-compose down — burns the entire Docker network with fire. The network and all the services contained within are totally destroyed. (edited)

Other commands

Docker for rails applications

Small gotchas

Image and Container

  • An image is an executable package that includes everything needed to run an application—the code, a runtime, libraries, environment variables, and configuration files. (you can create an image from a Dockerfile by running docker build).
  • Acontainer is a runtime instance of an image. A container is launched by running an image.

docker build

docker build -t container_name would make a tag which you can use to refer to the container.

docker-compose ports

ports:
  - '4000:80'

means you are mapping your machine’s port 4000 to the container’s published port 80.

apk command in Dockerfile.

docker-compose volumes

Main uses of volumes

  • (1) Immediate reflection of changes that are made in mounted files.
  • (2) The other use of volumes in Docker is for persistent data

Mounting files.

volumes:
  - ./:/app

means to mount the current local directory (./) into the container’s /app directory.

Data persistance

We can create a Docker volume and mount it in /var/lib/mysql of the database container. The life of this volume would be totally separate from the container lifecycle.
Compose can help us with managing these so-called named volumes.
They need to be defined under the volumes key in a compose file and can be used in a service definition.

version: "2"
services:
  mysql:
    image: mysql
    container_name: mysql
    volumes:
      - mysql:/var/lib/mysql
---
volumes: mysql:

.dockerignore

docker-compose down vs stop & up vs start

Run shell commands in containers

Networking in Compose

docker-compose up for only certain containers

Syntax of RUN in Dockerfile

In the shell form you can use a \ (backslash) to continue a single RUN instruction onto the next line. For example, consider these two lines.

RUN /bin/bash -c 'source $HOME/.bashrc; \
echo $HOME'

Together they are equivalent to this single line:

RUN /bin/bash -c 'source $HOME/.bashrc; echo $HOME'

Using attach

Use docker attach to attach your terminal’s standard input, output, and error (or any combination of the three) to a running container using the container’s ID or name.

$ docker attach <container_name/container_id>
$(docker-compose ps -q your-service)

Mount node_modules

I’ll leave an answer I found on StackOverflow here.

This happens because you have added your worker directory as a volume to your docker-compose.yml, as the volume is not mounted during the build.

When docker builds the image, the node_modules directory is created within the worker directory, and all the dependencies are installed there. Then on runtime the worker directory from outside docker is mounted into the docker instance (which does not have the installed node_modules), hiding the node_modules you just installed. You can verify this by removing the mounted volume from your docker-compose.yml.

A workaround is to use a data volume to store all the node_modules, as data volumes copy in the data from the built docker image before the worker directory is mounted. This can be done in the docker-compose.yml like this:

redis:
  image: redis
worker:
  build: ./worker
  command: npm start
  ports:
    - '9730:9730'
  volumes:
    - worker/:/worker/
    - /worker/node_modules
  links:
    - redis

I’m not entirely certain whether this imposes any issues for the portability of the image, but as it seems you are primarily using docker to provide a runtime environment, this should not be an issue.

Execute shell commands in containers

// Show the existing containers
$ docker-compose ps

// Execute the command the below with the container's name you want to run the shell commands in.
$ docker exec -i -t CONTAINER_NAME /bin/sh

Use binding pry in a docker container

Get into a running container

$ docker-compose run container-name bash

Edit files in a docker container

$ docker exec -u 0 -it <container_name> bash
$ apt-get update
$ apt-get install vim // Install the editor

Or use the following Dockerfile:

FROM  confluent/postgres-bw:0.1

RUN ["apt-get", "update"]
RUN ["apt-get", "install", "-y", "vim"]

Restore dump in your mysql db container

$ docker exec -i [mysql_container_name] mysql -u[username] -p[password] [DB name] < [path/to/sql/file]

What dose EXPOSE mean in Dockerfile?

The EXPOSE instruction exposes the specified port and makes it available only for inter-container communication.

If you want to EXPOSE in docker-componse, you can use -expose option.

About the author

I am a web-developer based somewhere on earth. I primarily code in TypeScript, Go and Ruby at work. React, RoR and Gin are my go-to Frameworks.