Skip to content

Instantly share code, notes, and snippets.

@justinmklam
Last active July 12, 2022 19:28
Show Gist options
  • Save justinmklam/6da23d6e19caee3d1e2352985ae0815f to your computer and use it in GitHub Desktop.
Save justinmklam/6da23d6e19caee3d1e2352985ae0815f to your computer and use it in GitHub Desktop.
Docker cheat sheet

Building

# Specify tag 
docker build -t mycontainer .

# Specify dockerfile
docker build -f Dockerfile .

Running

docker run mycontainer

# With environment variable
docker run --env MYVAR=true mycontainer

# With environment file
docker run --env-file .env mycontainer

# With custom command
docker run mycontainer sh -c "echo 'hello world'"

# Enter a shell inside the container (commands below are equivalent)
docker run -it mycontainer /bin/bash
docker run -it --entrypoint "/bin/bash" mycontainer

# Mount the current directory
docker run -v $(pwd):/home/src -it mycontainer /bin/bash

Advanced

# Get access to network control
docker run --privileged --net=host -it mycontainer /bin/bash

# Get access to systemd control (not recommended though)
docker run --privileged -v /bin/systemctl:/bin/systemctl -v /run/systemd/system:/run/systemd/system -v /var/run/dbus/system_bus_socket:/var/run/dbus/system_bus_socket -v /sys/fs/cgroup:/sys/fs/cgroup -it mycontainer /bin/bash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment