Table of Contents
- Great Resources
- Great introductory posts about Docker
- Official docker documents
- Docker Commands
- Docker Compose commands
- Docker for rails applications
- Small gotchas
- Use binding pry in a docker container
- Get into a running container
- Edit files in a docker container
- Restore dump in your mysql db container
- What dose EXPOSE mean in Dockerfile?
Great Resources
Great introductory posts about Docker
- Docker 101: Fundamentals & The Dockerfile – Paige Niedringhaus – Medium
- Docker 102: Docker-Compose – Paige Niedringhaus – Medium
- Dockerfile Explained
Official docker documents
- Dockerfile best practices
- Dockerfile Documentation
- Docker CLI Documentation
- Docker Compose Documentation
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 containersdocker stop
– Stops one or more running containersdocker build
– Builds an image form a Docker filedocker pull
– Pulls an image or a repository from a registrydocker push
– Pushes an image or a repository to a registrydocker export
– Exports a container’s filesystem as a tar archivedocker exec
– Runs a command in a run-time containerdocker search
– Searches the Docker Hub for imagesdocker attach
– Attaches to a running containerdocker commit
– Creates a new image from a container’s changesdocker rmi -f $(docker images -q)
– Delete all the existing images on your system. (-f
is theforce
option.)docker image rm [OPTIONS] IMAGE [IMAGE...]
- Delete specific images.docker prune
- Remove unused data
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 indocker-compose stop
— stops the network and saves the state of all the servicesdocker-compose start
— restarts the services and brings them back up with the state they had when they were stoppeddocker-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
- Quickstart: Compose and Rails | Docker Documentation
- Developing a Ruby on Rails app with Docker Compose – FireHydrant – Medium
- GitHub - K-Sato1995/docker_practice
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 aDockerfile
by runningdocker build
). - A
container
is a runtime instance of an image. Acontainer
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
- Understanding Volumes in Docker
- docker-compose.yml の volumes って何してるの?
- Docker Volumes and Networks with Compose
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 Official: Networking in Compose
- Stackoverflow: docker-compose make requests between containers
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.