FalseCAM
FalseCAM

Reputation: 55

Docker Compose: Avoid recreation of data container

To update some images I used 'docker-compose pull'. Then I build: 'docker-compose build'.

I wanted only to update the Application Container so I removed it and restarted: 'docker-compose rm app' and 'docker-compose up -d app'.

But something unwanted happened. The data container was recreated too. The old data is lost.

Dockerfile for Datacontainer:

FROM gitlab/gitlab-ce:latest
VOLUME ["/etc/gitlab", "/var/log/gitlab", "/var/opt/gitlab"]
ENTRYPOINT ["hostname"]

docker-compose.yml:

version: '2'
services:
 gitlab:
  image: 'gitlab/gitlab-ce:latest'
  domainname: example.com
  hostname: gitlab
  networks:
   - devenv
  restart: always
  environment:
   GITLAB_OMNIBUS_CONFIG: |
    external_url 'http://gitlab.example.com'
    gitlab_rails['gitlab_shell_ssh_port'] = 2224
  ports:
   - '80:80'
   - '2224:22'
  volumes_from:
   - gitlabdata

 gitlabdata:
  build: gitlab-data

How can I avoid this next time?

Upvotes: 3

Views: 3871

Answers (3)

Gonzalo
Gonzalo

Reputation: 13

I'm facing an issue with this, in my case, always I restart the docker-compose service, the container is recreated:

My /docker-compose-app.service

[Unit]
Description=Docker Compose Application Service
Requires=docker.service
After=docker.service

[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=/srv/dockercnf
ExecStart=/usr/local/bin/docker-compose up --no-recreate -d
ExecStop=/usr/local/bin/docker-compose down
TimeoutStartSec=0

[Install]
WantedBy=multi-user.target

The docker-compose.yml

version: '2'
services:
  mysql2:
    image: mysql/mysql-server:8.0
    container_name: mysql2-container
    networks:
      static-network:
        ipv4_address: 172.18.1.2
  mysql57:
    image: mysql/mysql-server:5.7
    container_name: mysql57-container
    networks:
      static-network:
        ipv4_address: 172.18.1.3
networks:
  static-network:
    ipam:
      config:
        - subnet: 172.18.0.0/16
          #docker-compose v3+ do not use ip_range
          ip_range: 172.18.1.0/24

I don't know what I'm missing or doing wrong.

Edited - Solved

After reading StackOverFlow Thread and getting my own conclusion, I edited my docker-compose.yml file as follows:

version: '2'
services:
  mysql2:
    image: mysql/mysql-server:8.0
    container_name: mysql2-container
    networks:
      static-network:
        ipv4_address: 172.18.1.2
  mysql57:
    image: mysql/mysql-server:5.7
    container_name: mysql57-container
    restart: always
    volumes:
        - /srv/mysql57-container:/var/lib/mysql
    networks:
      static-network:
        ipv4_address: 172.18.1.3

networks:
  static-network:
    ipam:
      config:
        - subnet: 172.18.0.0/16
          #docker-compose v3+ do not use ip_range
          ip_range: 172.18.1.0/24

Upvotes: 0

Ken Cochrane
Ken Cochrane

Reputation: 77335

Your issue was because you created a volume for a container, and then removed the container, this also removed your volumes.

You should change your volumes so that they bind mount to a host directory, this way your files are stored on the host, and you can reattach to those directories incase the container goes away. Another benefit is the ability to get access to those files from the host.

Here is roughly what your compose file would look like with the new volume config.

version: '2'
services:
   gitlab:
      image: 'gitlab/gitlab-ce:latest'
      domainname: example.com
      hostname: gitlab
      networks:
         - devenv
      restart: always
      environment:
         GITLAB_OMNIBUS_CONFIG: |
         external_url 'http://gitlab.example.com'
         gitlab_rails['gitlab_shell_ssh_port'] = 2224
      ports:
          - '80:80'
          - '2224:22'
      volumes_from:
          - gitlabdata

  gitlabdata:
      build: gitlab-data
      Volumes:
          - /etc/gitlab:/dir/on/host
          - /var/log/gitlab:/dir/on/host2
          - /var/opt/gitlab:/dir/on/host3

You can mount to what ever you want on the host. More info about volumes here: https://docs.docker.com/engine/userguide/containers/dockervolumes/#mount-a-host-directory-as-a-data-volume

Upvotes: 0

nessuno
nessuno

Reputation: 27052

The docker-compose up command has the --no-recreate flag. This flag avoid to recreate containers if they already exists.

Therefore you can run

docker-compose up -d --no-recreate app

Upvotes: 5

Related Questions