Skip to content

Instantly share code, notes, and snippets.

@ljaraque
Last active September 7, 2018 02:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ljaraque/1efd552adbed3356962646f75ad0d741 to your computer and use it in GitHub Desktop.
Save ljaraque/1efd552adbed3356962646f75ad0d741 to your computer and use it in GitHub Desktop.
Docker Cheatsheet

Docker Cheatsheet

ljaraque@yahoo.com

1. Docker Basics

list all docker running containers:
docker ps

list all docker containers running and stopped:
docker ps -a

list all docker images:
docker images

stop all containers (Brute Force):
docker kill $(docker ps -q)

stop all containers (Smooth Method):
docker stop $(docker ps -a -q)

remove all containers:
docker rm $(docker ps -a -q)

remove all docker images:
docker rmi $(docker images -q)

log into a container:
docker exec -ti <container's hash> /bin/bash

log into a container as root:
docker exec -it --user root <container's hash> /bin/bash

copy files from/to a container:
docker cp <container_hash>:/destination/path .
docker cp <files_to_copy> <container_hash>:/destination/path

2. Docker Compose

docker-compose build
docker-compose up

3. Creating a Dockerfile

FROM
defines the image in which the image will be based on. it is the first command declared in a dockerfile.
it will take the image from host computer or from DockerHub:

# Usage: FROM [image name]
FROM ubuntu

MAINTAINER
credits of creator or maintainer:

# Usage: MAINTAINER [name]
MAINTAINER authors_name

ADD
places files in a docker image, taking it from host computer or from url:

# Usage: ADD [source directory or URL] [destination directory]
ADD /my_app_folder /my_app_folder

RUN
runs a command at the moment of building an image.
used to install dependencies:

# Usage: RUN [command]
RUN aptitude install -y riak

CMD
runs a command at the moment of initiating a container:

# Usage 1: CMD application "argument", "argument", ..
CMD "echo" "Hello docker!"

ENTRYPOINT
defines the application in which the commands specified buy CMD:

# Usage: ENTRYPOINT application "argument", "argument", ..
# Remember: arguments are optional. They can be provided by CMD
#           or during the creation of a container. 
ENTRYPOINT echo

# Usage example with CMD:
# Arguments set with CMD can be overridden during *run*
CMD "Hello docker!"
ENTRYPOINT echo

ENV
sets an environment variables in key = value pair:

# Usage: ENV key value
ENV SERVER_WORKS 4

EXPOSE
defines port to communicate to another container:

# Usage: EXPOSE [port]
EXPOSE 8080

USER
specifies uid or username that will run the container:

# Usage: USER [UID]
USER 751

VOLUME
allows access from container to host computer:

# Usage: VOLUME ["/dir_1", "/dir_2" ..]
VOLUME ["/my_files"]

WORKDIR
specifies in which folder CMD commands should be executed:

# Usage: WORKDIR /path
WORKDIR ~/

Example dockerfile
Dockerfile to run a flask app called flask-docker.py will look like:

FROM ubuntu:latest
MAINTAINER developer "developer@domain.com"

RUN apt-get update -y
RUN apt-get install -y python-pip python-dev build-essential

ADD flask-app /flask-app
WORKDIR /flask-app
RUN pip install -r requirements.txt

ENTRYPOINT ["python"]
CMD ["flask-docker.py"]

Build the Docker Image:
https://docs.docker.com/engine/reference/commandline/build/
To create the image just run:

docker build -t image_name:latest .

Run the Docker Image:
https://docs.docker.com/engine/reference/commandline/run/

docker run -d -p <host's port>:<container's port> image_name

References:

  1. Dockerize a Flask Project
  2. Dockerize Simple Flask App
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment