Skip to content

Instantly share code, notes, and snippets.

@MikyStar
Last active September 18, 2018 14:03
Show Gist options
  • Save MikyStar/2aca6274c27b440773d176d8c7cde217 to your computer and use it in GitHub Desktop.
Save MikyStar/2aca6274c27b440773d176d8c7cde217 to your computer and use it in GitHub Desktop.

Usefull informations

Image

An Image is a snapshot of one or multiple Containers

Container

It can be a name, the container full id or just a the begining string from container id long enough to be different from other containers on the machine

Swarm

The Swarm mode let us use multiple machines to create one container

Networks

For each service you can define one or multiple networks and at the end of the docker-compose file you tell every networks of your project. Saying that a service belongs to a specific network enable them to access services inside the specified networks

Depends on

If a service needs an other one to be launched first, it has to be said in the depends_on section

Dockerfile

The Dockerfile is basically the file that "parse" my local version of the code to the future folder on the server where my code will be executed. It contains instruction to setting up the environment.

Note : The WORDIR command only change the working directory for RUN commands, otherwise, ADD, COPY and so on are not impacted.

docker-compose.yml

It's the file containing containers of my project (if I'm using multiple containers) such as NodeJS, PHP, MongoDB, MySQL, Python ... It's will link them together to create our final Docker Image

Commands

Creating a Docker Machine to run your image

It's not necessary to run an image nor a container, it provides an other IP adress thant the default 0.0.0.0

For Mac

docker-machine create --driver virtualbox [ name of the machine ] # If there is no machine on your system, call your first one default
docker-machine env [ name of the machine]
eval "$(docker-machine env default)" # To connect your shell to the new machine

Listing all the machines on your system

docker-machine ls

Building a container

docker-compose build

Start/Stop container

docker start [ container ]
docker stop [ container ]

Removing container

docker rm [ container / list of containers ]

Listing containers

docker ps # With -a you can see all containers, without, just actives ones

Listing images

docker images

Remove an image

docker rmi [ image ]

Remove all images

docker rmi $(docker images -a -q)

Remove all volumes

docker volume rm $(docker volume ls -q)

Remove all containers

docker rm $(docker ps -a -q)

The 'run' command

docker run [ image ] # Run a container
docker run -it [ image ] [ command ] # Run and launch a command inside the container in interactive mode; using 'bin/bash' it opens a shell inside
docker run --name [ name ] [ image ] # Run and name a container
docker run -e [VARIABLE_NAME]=[KEY] [ image ] # Run and set a environment variable to the container
docker run -d [ image ] # Run in detacher mode -> the terminal won't be connected to the container -> no commands, no prompt ...
docker run -P [ image ] # Run and all the exposed ports in the container will be published to random ports of the host
docker run -p [port on my physical machine]:[port inside the container] # Run and map the port on my physical machine to the port inside the container

Reinitializing an image

docker-compose rm

Get a list of all containers

# Only running containers
docker ps

# All containers
docker ps -a

Get a list of all the images stored

docker images

Create a container out of an image

docker run [ image name ][optional ":"tag name]

Rename a container

docker rename [ previous name ] [ new name ]

Run a container

docker-compose up

Launch a docker machine

docker-machine start [ name of machine ]

Get the IP address of the images running on the machine

docker-machine ip [ name of the image ]

Deleting images

# Delete an image
docker rmi [ name of the image ]

# Delete all images
docker rmi $(docker images -q)

Delete a container

docker rm [ name or ID of the container ]

Pushing an image to a Docker Cloud repository

docker login
docker tag [ image ] [ Docker ID ]/[ image ][:tag]
docker push [ Docker ID ]/[ image ][:tag]

Note : The tag is what is going to be visible in the Docker Cloud to identify your commit

Interacting with what's encapsulated inside a container

# If the container is running
docker exec -it [optional --name 'name I want to give'] [optional -u 'user' to launch the command like a specific user] [ name of container ] [ the command I want to launch ]
# NB : If we enter exit to leave the container, it will still be running in the background

# OR

# If the container is not running
docker run -it [optional --name 'name I want to give'] [ id of the container ] [ the command I want to launch ]
# NB : If we enter exit to leave the container, it will be stopped as well

With the command 'bash' we open a shell inside the container, and we can browse, edit, install stuffs ...

Launching a container

docker start [ name or ID of the container ]

For example if we want to launch a service

docker stop [ name or ID of the container ]
# You can also restart it
docker restart [ name or ID of the container ]

Creating an image out of a container

docker commit [ container name or ID ] [ how I want my image to be named ]

Copy files between Docker container and machine

docker cp [location] [destination] # Path within a container = [container]:[path of the file]

Exposing ports

docker run -td --name [name I want to give] -p [ports I\'m exposing] [ name of the image ]

Knowing the ports of a container

docker port [ container ]

Mounting folder to a container

docker run -it --name [name I want to give] -v [absolute path of what I want to mount]:[absolute path where I want to mount it in the container]

Volumes

If we mount a volume to a container and we delete this container, the volume will still exists, so we can create an other container and mounting the volume to it, this way we make persistance

# Listing all volumes
docker volume ls

# Mounting a volume to a container
docker run -it --name [name I want to give] -v [name we want to call it]:[absolute path where I want to mount it in the container]

# Mounting the volumes from a container to an other that that they can 'exchange' datas
docker run -it --name [name I want to give] --volumes-from [container where volumes are]

Getting informations on the configuration of a container

docker inspect [container]

Injecting environment variables to the container

docker run -it --name [name I want to give] -e [KEY]=[VALUE] [optional -e [KEY]=[VALUE]]

# Or with a file
docker run -it --env-file [path of the file]

# If we want to change a value at the fly like passing from dev to prof
docker run -it --env-file [path of the file] -e [KEY_I_WANT_TO_CHANGE]=[NEW_VALUE]

Pushing an image to Docker HUB

# After being logged
docker push [ name on Docker Hub ]/[ image name ]:[ tag ]

Pulling an image from Docker HUB

docker pull

Create a Swarm

docker swarm init

Run a Swarm

docker stack deploy [ name of the swarm ]
docker stack deploy --compose-file [ YAML file needed for the composing ] [ name of the swarm ] # To use a compose file

Visualize a Swarm

docker stack services [ name of the Swarm ]

Listing all the Swarms

docker stack ls

Removing a Swarm

docker stack rm [ name of the swarm ]

Removing the auto restart of a container

docker update --restart=no [ my-container ]
docker update --restart=unless-stopped [ my-container ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment