Skip to content

Instantly share code, notes, and snippets.

@eliezedeck
Last active April 26, 2016 14:04
Show Gist options
  • Save eliezedeck/92c5efe998bfed705062 to your computer and use it in GitHub Desktop.
Save eliezedeck/92c5efe998bfed705062 to your computer and use it in GitHub Desktop.
NAME='container-name'
# Things to know and understand
# - Docker needs to run at least one process inside, this is the command (CMD in Dockerfile)
# that defines the lifetime of the container
# - Docker will stop the container if the CMD stops
# - It is possible to run multiple processes inside one container (like from a shell)
# - Supervisor is a recommended tool to spawn and manage many processes
# Create + Run (run always creates a new container)
# -d daemonizes
# -P exports the ports from the container
# -v specifies volume mapping as in host:container
docker run -d -P --name $NAME -v "/home/ubuntu/docker:/data" rethinkdb
# -i runs in interactive mode (contrary to -d above)
# -t allocates a tty
# bash is the command (CMD) to be run. If bash exits, the container will be stopped.
docker run -i -t --name $NAME ubuntu bash
docker run -i -t --name $NAME ubuntu:latest bash
# Stop the container
docker stop $NAME
# Run only (after creation)
docker start $NAME
# -a reattaches, same as docker attach if it's already running
# -i interactive mode
docker start -a -i $NAME
# Save into a TAR file
# NOTE: Only an image can be saved into a tar file, not a container, so, first
# of all, create a container, create an image, then create the tar file
# 0a54a5c35c01 is the container ID
# ubuntu:mine is the image name to create (be sure to set :mine to something else, or not!)
docker commit -m 'Made changes to the image' -a 'Elie Zedeck' 0a54a5c35c01 ubuntu:mine
docker save -o image.tar ubuntu:mine
# Load from a TAR file
# NOTE: Like save, it only loads into an image, not a container
docker load -i image.tar ubuntu:mine
# IP address of the Container
docker inspect --format '{{ .NetworkSettings.IPAddress }}' $NAME
# Run another command in an existing container
# -i runs in interactive mode (contrary to -d above)
# -t allocates a tty
docker exec -i -t $NAME bash
# Daemon programs should be be interactive, and thus should not need a tty
docker exec $NAME daemon-program
# Use the same network as the Host so that things on the host are directly accessible inside the container
docker run --net=host -i -t --name $NAME ubuntu bash
# Remove the container
docker rm $NAME
# Stop all containers
docker stop $(docker ps -a -q)
# Delete all containers
docker rm $(docker ps -a -q)
# Delete all images
docker rmi $(docker images -q)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment