Docker - Anton-L-GitHub/Learning GitHub Wiki

Dockerfile

FROM <image:version>    # Never use :latest
WORKDIR <path>          # Change working dir
COPY <what> <where>
ADD <what> <where>      # Same as copy, but can take URL and ZIP.
RUN <cmd>               # Run command inside container.
ENV <VARIABLE=VALUE>    # Set environment variable
EXPOSE <port>           # Container should start on a given port.
USER <user>             # Switch to another user. Place in near beginning to avoid problems. 
    # RUN addgroup app && adduser -S -G app app
    # USER app
CMD <cmd>               # Starting command for container.
    # Can also use ENTRYPOINT, which makes it harder to start with other command.

Images

## Tag
docker image tag my-app:latest my-app:2         # Never use :latest!
docker build -t my-app:1 .

## .tar save & load
docker image save -o my-app.tar my-app:latest   # Save to .tar file
docker image load -i my-app.tar                 # Load from .tar

Containers

# Run
docker run <container>
docker run -d <container>                   ## Detached mode
docker run -d --name blue-sky <container>   ## Give container a name.
docker run -d -p 80:3000 <container>        ## Portmap 80 on host to 3000 on container.
docker start <container>                    ## Start a stopped container

# Stop
docker stop <container>

# Remove
docker rm <container>

# Interact
docker exec -it <container> sh             ## Open shell in container.

# Prune
docker container prune

# Logg
docker logs <container> 
docker logs -f <container>                  ## Follow the log
docker logs -n 5 <container>                ## Last 5 lines

docker inspect <container>

Volymes

  • Storage outside of the containers.
docker volume ls
docker volume create app-data
docker volume inspect app-data
docker run -v app-data:/app/data <image>

Copying files between host and cointainers

docker cp <containerID>:/app/log.txt .
docker cp secret.txt <containerID>:/app

Sharing source code with containers

docker run -v $(pwd):/app <image>

Optimize builds

  • Docker uses layer.
    • If there is no change in a layer, docker takes layer from cache instead of buildning it again.
  • Have this in mind when designing your builds dockerfile.

Build docker files with most:

Stable instructions (that never change)
|
|
|
▼
Changing instructions (like copying code)

Containrar är endast "öppna" så länge som den "gör något".

Attach - Din console är kopplad till ouputen från containern
Detach - Denna consol är inte connected till outputen


Containers

Run container

docker run <container/id>

-d - Detach mode, docker attach <container/id> för att komma tillbaka.
-i - Interactive mode, pip:ea/knyt din terminal till docker img input. (Docker använder din termianl som input)
-it - Interactive terminal där coker även frågar terminalen efter input

Stop container

docker stop <container/id>

docker stop $(docker ps -aq) - Stoppa alla containrar

List cointainers

docker ps 

-a - Alla, tom. stoppade containrar -aq - Alla (IDs Only)

Inspect cointainer
Returnar en json med data om containern.
Bra för att se var datan är pipead. (volume mapping)

docker inspect <container/id>

Remove permanently

docker rm <container/id>

docker rm $(docker ps -aq) - Radera alla containrar


Images

List images

docker images

Pull Image

docker pull <image-name>

Remove images

docker rmi <image-name>

OBS! Du måste ha raderat alla dependent containers för att kunna ta bort en image


Kör ett commando inuti en container

docker exec <container/id> command

Volume Mapping

Är när man knyter input till en container till en dir på Host. Exempel:
Datan i en MySQL försvinner när containern stängs ner så kan man pipea datan till en dir på Host.

docker run -v /opt/datadir:/var/lib/mysql mysql

Port mapping

⚠️ **GitHub.com Fallback** ⚠️