Docker: Mixed topics


Installing Docker

Installing Docker on Ubuntu 20.04

sudo apt install docker.io

Container configuration

Network configuration

Adding entries to containers /etc/hosts file

You can add a new entry to /etc/hosts file of a Linux based docker container by using the –add-host option of the docker run command:

docker run --add-host somedomainname:someipaddesssomeiamge

Handling images

Showing Docker images

docker images

Deleting docker images

Removing of all unused or unreferenced images, containers, volumes and networks

Docker offers the following command for removal of unreferenced images, containers, volumes and networks:

docker system prune

And the following command for removal of unused images, containers, volumes and networks:

docker system prune -a

Delete specific images:

docker image rm IMAGE

Handling Containers

Showing containers

Showing all containers:

docker container ls -a

Showing containers using filters:

docker ps -a -f status=created

Delete Containers

Delete a specific container:

docker rm -f Container_ID

Deleting all stopped containers:

docker container prune

Copy files from containers

Copy files from running or stopped containers

$Tag.Docker.CopyFiles.FromRunningContainer, $Tag.Docker.CopyFiles.FromStopedContainer

The following command allows to copy files from inside containers, regardless whether they are running or stopped:

docker container cp container_name:path_to_file_inside_container path_destination

Mounting folders

How internal (container) command can operate on external (host) file or folder?

we've had a problem here:
[soshsc show=”logged” role=”administrator”] ... [/soshsc]
invalid attribute(s) a/o value(s)

Mounting, if done in the following way, allows the internal (container) running command/process to access and operate on external (host) files/folders.

The following example shows how this can work:

Here we have a complex container command with parameters which is executed by the docker run command. The complex command operate on two files among other things. The way we mount our current folder on the host site into the container, results in the complex container command to operate on those two files on the host side. If the current path on the host is something like /home/vsts/work/1/s, then the mounting option -v ${pwd}:${pwd} ensures that a path with the same structure (/home/vsts/work/1/s) is created inside the container with the corresponding host path mounted on it. The option -w ${pwd} then ensures that the working directory inside the container is set to the same path (/home/vsts/work/1/s). When the command/process inside the container starts to operate, it will be operating against the internal working directory which is now the same as the external current folder because of the way we have mounted that folder and set the internal working directory:

docker run -v ${pwd}:${pwd} -w ${pwd} –name pactcontainer pactfoundation/pact-cli pactflow publish-provider-contract ${pwd}/openapi/swagger.json –broker-base-url https://yourcompany.pactflow.io –broker-token $PACT-TOKEN –provider provider-x –provider-app-version $(Build.SourceVersion) –branch $(Build.SourceBranchName) –content-type ‘application/yaml’ –verification-exit-code=0 –verification-results ${pwd}/openapi/testresults.txt –verification-results-content-type ‘text/plain’ –verifier dredd –verbose

Executing commands inside a running container

If you have a running container, for example a container which has a web application running, you can access the containers bash and execute commands inside the container and see the results.

To do that, first let all running containers being shown:

docker container list -a

Then write down the ID of the container you want to run a command inside and execute the following command:

docker exec -it your_container_id /bin/bash

Then a special prompt appears which indicates that your are now running commands inside the container. Form here you can execute any command which does exist in the container.

Docker host

Connecting the docker host

You can access the docker host by using the docker host’s IP address, but as of Docker version 18.03 you can alternatively use the address host.docker.internal to connect to your docker host from inside the docker container.

Logging

Showing RUN command logs during docker image build

we've had a problem here:
[soshsc show=”logged” role=”administrator”] ... [/soshsc]
invalid attribute(s) a/o value(s)

Earlier versions of the docker have shown a more extensive log output which also included the logs output of individual RUN commands. But newer version of docker used BuildKit which has a kind of shortened logs output. You can read more about this here:

https://docs.docker.com/develop/develop-images/build_enhancements/

If you want to see more detailed build output logs, you can deactivate the new BuildKit feature by added the following environment variable before the actual build command and also make sure that there is no caching (otherwise the commands would not be re-executed and there would be no logs for them) as follows:

sudo DOCKER_BUILDKIT=0 docker build -f Dockerfile.Cypress.Headed -t cypress/cypress-image-headed:1.0 . --progress=plain --no-cache

Troubleshooting

In Windows powershell dockerfile command outputs are not shown

Problem

When building a docker file, commands inside the docker file, like the following command are shown without their output, kind of like non verbose:

RUN type ./nuget.config

Resolution

For Powershell:

Set the following environment variable before building the docker file:

$env:BUILDKIT_PROGRESS='plain'

Error: docker: invalid reference format

Problem

Running a “docker run” command such as the following example leads to the error further below:

docker run -i –mount type=bind,source=/home/userx/Cypress/myapplication/cypress,target=/docker-cypress/cypress -w /docker-cypress -t –rm -e DISPLAY=”192.168.100.15:0.0″ -e CYPRESS_serverAddress= https://my-application-add/ cypress/cypress-image-headed:1.0 open

Error:

docker: invalid reference format. See ‘docker run –help’.

Resolution

In my case it was a space after “-e CYPRESS_serverAddress= ” which had to be removed:

docker run -i –mount type=bind,source=/home/userx/Cypress/myapplication/cypress,target=/docker-cypress/cypress -w /docker-cypress -t –rm -e DISPLAY=”192.168.100.15:0.0″ -e CYPRESS_serverAddress= https://my-application-add/ cypress/cypress-image-headed:1.0 open