Skip to content

Instantly share code, notes, and snippets.

@adrianlshaw
Last active May 29, 2021 15:03
Show Gist options
  • Save adrianlshaw/e07e5d75385315d96c24bdeba68c42ba to your computer and use it in GitHub Desktop.
Save adrianlshaw/e07e5d75385315d96c24bdeba68c42ba to your computer and use it in GitHub Desktop.
Docker cheatsheet

Make Dockerfile

Put a Dockerfile in the root of your project

Building an image

docker build -t myproject . 

The dot at the end tells Docker to look for the Dockerfile in the current directory

-t gives a name to the container image

Running the image

docker run -ti --rm myproject

The "-ti" is necessary to see the container console output.

The "--rm" deletes any data the container made after it finishes running.

If you need to expose a listening port

docker run -p 8080:80 -ti --rm myproject

Map TCP port 80 in the container to port 8080 on the Docker host.

Persistent data

When a container exits, all newly created data is lost. To persist data, the easiest way is to mount a shared directory between host and container

Use -v to specify the host:container paths to the shared directory

docker run -v $(PWD):/opt -ti myproject 

This mounts the host working directory ($PWD) into the container's /opt directory

Debugging

If you just want to mess about in empty container then you can just start a blank container

docker run -ti --rm ubuntu 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment