gai-jin
gai-jin

Reputation: 853

How to keep docker container settings given by run command arguments?

I start a container from a docker image using docker run command + many arguments, like:

docker run -d --name=ubu2 --restart=always --pids-limit 400 --memory=3g --memory-swap=6g --cpus="2.0" -p 127.0.0.1:8585:8585 -v /apps:/apps ubuImage

when I execute

docker inspect ubu2

I can correctly see my settings in the description of the container.

When I restart the container using simply docker restart ubu2, I can still see my settings. Is this the expected behaviour? Shouldn't the container start fresh, without the settings from the run command arguments?

In general what is the best way to keep such kind of settings in the definition of the container? More - if I want to change the value of any of them, let's say memory to 4GB - what is the appropriate way to do this? docker container update?

Upvotes: 2

Views: 1294

Answers (1)

David Maze
David Maze

Reputation: 158848

A Docker container wraps a single process. docker restart restarts the process, without recreating the container environment; that's why the container settings are preserved across a container restart.

In general I don't usually bother stopping and starting containers; just delete and recreate them. Then you're sure of starting from a clean environment.

docker stop ubu2
docker rm ubu2
docker run -d --name ubu2 ...

If you have a consistent set of docker run options you use, you can put them in a shell script (like any other shell command), or use a tool like Docker Compose that encapsulates a set of container options in a file. The command you show could be written in Compose syntax like:

# docker-compose.yaml
version: '2.4'       # v3 doesn't support single-container resource constraints
services:
  ubu2:
    # build: .       # if you'd normally `docker build` the image
    image: ubuImage  # not required if `build:` is given
    restart: always
    pids_limit: 400
    mem_limit: 4g
    memswap_limit: 6g
    cpus: 2.0
    ports:
      - 127.0.0.1:8585:8585
    volumes:
      - /apps:/apps

If you run docker-compose up -d, it will create a container (in the background) matching this template. If you change one of these values, and run docker-compose up -d again, it will delete and recreate containers as required to have the updated settings.

Upvotes: 1

Related Questions