Skip to content

Instantly share code, notes, and snippets.

@jonathanagustin
Created April 29, 2020 06:24
Show Gist options
  • Save jonathanagustin/2e4ae44c840a1054a51f16d64aae7815 to your computer and use it in GitHub Desktop.
Save jonathanagustin/2e4ae44c840a1054a51f16d64aae7815 to your computer and use it in GitHub Desktop.
Docker

Docker

Lessons Learned

/root/compile: line 2: $'\r': command not found

The above means you need to dos2unix the file.

Useful commands

Changing permissions and executing a file

# change permissions on the compile script
RUN chmod +x /root/compile.sh

# test out compile command
RUN /bin/bash -c "source /root/compile.sh"

How to copy and overwrite a file

For example, if you have a custom configuration file, in the assets folder and you want to copy and overwrite a system configuration, use the COPY command inside the Dockerfile:

COPY assets/configuration.conf /etc/configuration.conf

SSH into Docker Container

The argument -it means run in interactive mode (hence the -i) and allocate a pseudo-TTY (TTY means "teletypewriter", hence the -t). Teletypewriter mode allows one to send inputs to the container.

docker exec -it <CONTAINER NAME> sh

Stop and Remove All Containers

docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)

Delete images and containers

# Delete every Docker containers
# Must be run first because images are attached to containers
docker rm -f $(docker ps -a -q)

# Delete every Docker image
docker rmi -f $(docker images -q)

Create Static IPs for Dockerfiles

First create your own docker network:

docker network create --subnet=172.19.0.0/16 <NETWORK NAME>

The subnet can be any IP address that does not conflict with any other IP address. I just used 172.19.0.0/16 as an example.

Use any name to name the <NETWORK NAME>.

Next, run your Dockerfile with the created network:

docker run --net <NETWORK NAME> --ip 172.19.0.22 -it <CONTAINER NAME> bash

Override DNS settings

To achieve this, you need to change the DNS settings of the Docker daemon. You can set the default options for the docker daemon by creating a daemon configuration file at /etc/docker/daemon.json.

You should create this file with the following contents to set two DNS, firstly your network’s DNS server, and secondly the Google DNS server to fall back to in case that server isn’t available:

/etc/docker/daemon.json:

{
    "dns": ["10.0.0.2", "8.8.8.8"]
}

Then restart the docker service:

sudo service docker restart

Show versions of a package through yum

yum --showduplicates list <PACKAGE>| expand

Docker Links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment