Skip to content

Instantly share code, notes, and snippets.

Created October 29, 2020 05:41
Show Gist options
  • Save harikt/d6d686ad2593a5293be547a38d27b7fe to your computer and use it in GitHub Desktop.
Save harikt/d6d686ad2593a5293be547a38d27b7fe to your computer and use it in GitHub Desktop.
Anatomy of a Dockerfile notes for myself from here:

Set the base image to Ubuntu must be first instruction - use docker search to find images

FROM ubuntu # <image>
FROM ubuntu:latest # - <image>:<tag>
FROM ubuntu:precise (LTS)

Set the maintainer info

MAINTAINER Adam Veldhousen, # <name>

Copy a local/remote file into the container Also supports auto extracting of .tar.gz into destination and can copy from remote locations (ie raw.github)

ADD ./index.html /var/www/index.html # <src> <dest>

Prefer copy over add: Copy only supports copying local files

COPY ./index.html /var/www/index.html

Sets the user name to use

USER username

ENV set env vars

ENV KEY 1234 # <key> <value>

Runs a command on the image and commits the result

RUN apt-get install vim # <command> - equivalent to docker run ubuntu apt-get install vim && docker commit XXXX

The WORKDIR directive is used to set where the command defined with CMD is to be executed.

WORKDIR /home/dev

Like an exec, preferred (A minimal $PATH only)

ENTRYPOINT ["executable", "param1", "param2"] 

Execute as a shell (Your $PATH is setup this way)

ENTRYPOINT command param1 param2 


CMD ["-l","-"]
ENTRYPOINT ["/usr/bin/wc"]

and this:


and this:

ENTRYPOINT ["wc", "-l", "-"]

are all equivalent

Exposes this port on the container to the outside world

EXPOSE 80 # <port> [<port>...]

Adds one or more new volumes to any container created from the image

VOLUME ["/data"] # [<volumes>...], puts /data -> /var/lib/docker/volumes/

If you're using boot2docker, and you would like to access your container via localhost, you can run the following line: VBoxManage controlvm boot2docker-vm natpf1 ",tcp,,,,"

This adds a port forwarding rule to virtual box that passes the local host traffic through the boot2docker vm and into the container.

To build a dockerfile

docker build # current directory, uses dockerfile in cwd -> docker build . is equivalent
docker build - < dockerfile # stdin
docker build # github

The -t option tags your image

docker build -t memcached 

To run your image

docker run memcached # run your image

And this:

docker run -d -p 8080:80 myapp 

Runs your image that you tagged "myapp" in the background (-d) and directs traffic from port 8080 to your container's port 80 (-p 8080:80 AKA :)

use docker ps to list running containers

Run and mount the folder on the host named '/home/adam/data' to a folder in the container accessible at '/data/'

docker run -v /home/adam/data:/data debian ls /data

Inspect the volumes for running containers

docker inspect -f {{.Volumes}} container-test

You either need to tag it as <myuser>/hellodocker when you build it, e g

docker build -t <myuser>/hellodocker:mytag .

or create a new tag tied to the same image, i e

docker tag hellodocker:mytag <myuser>/hellodocker:mytag

and then docker push

docker push <myuser>/hellodocker:mytag
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment