Docker Primer

Docker Primer

Run a background MySQL container
Background containers are how you’ll run most applications.
docker container run --detach --name mydb -e MYSQL_ROOT_PASSWORD=my-secret-pw mysql:latest
–detach will run the container in the background.
–e will use an environment variable to specify the root password

Shows logs from MySQL Docker container.
docker container logs mydb

Processes running inside the container.
docker container top mydb
Although MySQL is running, it is isolated within the container because no network ports have been published to the host. Network traffic cannot reach containers from the host unless ports are explicitly published.

Connect to a new shell process inside an already-running container.
docker exec -it mydb sh
Executing the command below will give you an interactive shell (sh) inside your MySQL container.

Check version number
mysql --user=root --password=$MYSQL_ROOT_PASSWORD --version

Bind mount
When you’re actively working on an application it is inconvenient to have to stop the container, rebuild the image, and run a new version every time you make a change to your source code.
One way to streamline this process is to mount the source code directory on the local machine into the running container. This will allow any changes made to the files on the host to be immediately reflected in the container.
We do this using something called a bind mount.
When you use a bind mount, a file or directory on the host machine is mounted into a container running on the same host.

docker container run --publish 80:80 --name linux_tweet_app --mount type=bind,source="$(pwd)",target=/usr/share/nginx/html $DOCKERID/linux_tweet_app:1.0

docker-compose up –help:

Builds, (re)creates, starts, and attaches to containers for a service. Unless they are already running, this command also starts any linked services.
The `docker-compose up` command aggregates the output of each container. When the command exits, all containers are stopped. Running `docker-compose up -d` starts the containers in the background and leaves them running.
If there are existing containers for a service, and the service’s configuration or image was changed after the container’s creation, `docker-compose up` picks up the changes by stopping and recreating the containers (preserving mounted volumes). To prevent Compose from picking up changes, use the `–no-recreate` flag.
If you want to force Compose to stop and recreate all containers, use the `–force-recreate` flag.
Usage: up [options] [–scale SERVICE=NUM…] [SERVICE…]
-d, –detach Detached mode: Run containers in the background, print new container names. Incompatible with –abort-on-container-exit.
–no-color Produce monochrome output.
–quiet-pull Pull without printing progress information
–no-deps Don’t start linked services.
–force-recreate Recreate containers even if their configuration and image haven’t changed.
–always-recreate-deps Recreate dependent containers.
Incompatible with –no-recreate.
–no-recreate If containers already exist, don’t recreate
them. Incompatible with –force-recreate and -V.
–no-build Don’t build an image, even if it’s missing.
–no-start Don’t start the services after creating them.
–build Build images before starting containers.
–abort-on-container-exit Stops all containers if any container was
stopped. Incompatible with -d.
-t, –timeout TIMEOUT Use this timeout in seconds for container
shutdown when attached or when containers are
already running. (default: 10)
-V, –renew-anon-volumes Recreate anonymous volumes instead of retrieving
data from the previous containers.
–remove-orphans Remove containers for services not defined
in the Compose file.
–exit-code-from SERVICE Return the exit code of the selected service
container. Implies –abort-on-container-exit.
–scale SERVICE=NUM Scale SERVICE to NUM instances. Overrides the
`scale` setting in the Compose file if present.

docker-compose –help:
Define and run multi-container applications with Docker.

docker-compose [-f …] [options] [COMMAND] [ARGS…]
docker-compose -h|–help

-f, –file FILE Specify an alternate compose file (default: docker-compose.yml)
-p, –project-name NAME Specify an alternate project name (default: directory name)
–verbose Show more output
–log-level LEVEL Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
–no-ansi Do not print ANSI control characters
-v, –version Print version and exit
-H, –host HOST Daemon socket to connect to

–tls Use TLS; implied by –tlsverify
–tlscacert CA_PATH Trust certs signed only by this CA
–tlscert CLIENT_CERT_PATH Path to TLS certificate file
–tlskey TLS_KEY_PATH Path to TLS key file
–tlsverify Use TLS and verify the remote
–skip-hostname-check Don’t check the daemon’s hostname against the
name specified in the client certificate
–project-directory PATH Specify an alternate working directory
(default: the path of the Compose file)
–compatibility If set, Compose will attempt to convert keys
in v3 files to their non-Swarm equivalent

build Build or rebuild services
bundle Generate a Docker bundle from the Compose file
config Validate and view the Compose file
create Create services
down Stop and remove containers, networks, images, and volumes
events Receive real time events from containers
exec Execute a command in a running container
help Get help on a command
images List images
kill Kill containers
logs View output from containers
pause Pause services
port Print the public port for a port binding
ps List containers
pull Pull service images
push Push service images
restart Restart services
rm Remove stopped containers
run Run a one-off command
scale Set number of containers for a service
start Start services
stop Stop services
top Display the running processes
unpause Unpause services
up Create and start containers
version Show the Docker-Compose version information

Common Commands:
IP address of running docker container

docker inspect -f "{{ .NetworkSettings.IPAddress }}" $containerId$

List the running containers.

docker container ls

re-build all the containers.

docker-compose up -d --no-deps --build

Common Errors and their solutions:
Question: “Bind for failed: port is already allocated” error
Answer: You need to make sure that the previous container you launched is killed, before launching a new one that uses the same port.

docker container ls
docker rm -f $container-name$

Question: ERROR: for $serviceName$ Cannot start service $serviceName$: driver failed programming external connectivity on endpoint $EndpointName$ : Error starting userland proxy: listen tcp bind: An attempt was made to access a socket in a way forbidden by its access permissions.
ERROR: Encountered errors while bringing up the project.
Answer: Check your docker-compose.yml file or docker-compose.override.yml file. if the ports node is using $PortNumber$, check if this port is present as a part of the reserved port by using the command

netsh int ip show excludedportrange protocol=tcp

. If yes, then change the $PortNumber$ in the docker-compose.yml file or docker-compose.override.yml file, to a nonreserved $PortNumber$.