docker-compose ps does not show running services - docker

I have a docker-compose stack launched on a remote machine, through gitlab CI/CD (a runner connects to the docker engine on the remote machine and performs the deploy with docker-compose up -d).
When I connect to that machine from my laptop, using eval docker-machine env REMOTE_ADDRESS, I can see the docker processes running (with docker ps), while the services stack appears to be empty (docker-compose ps).
I am not able to use docker-compose down to stop the stack, and trying docker-compose up -d gives me the error
ERROR: for feamp_postgres Cannot create container for service postgres: Conflict. The container name "/feamp_postgres" is already in use by container "40586885...". You have to remove (or rename) that container to be able to reuse that name.
The reverse is also true, I can start the stack from my local laptop (using docker-machine), but then the CI/CD pipeline fails when trying to execute docker-compose up -d with the same error.
This happens using the latest versions of docker and docker-compose, both on the laptop (OSX) and on the runner (ubuntu 18.04).
In other circumstances (~10 other projects) this has worked smoothly.
This is the docker-compose.yml file I am using.
version: "3.7"
services:
web:
container_name: feamp_web
restart: always
image: guglielmo/fpa/feamp:latest
expose:
- "8000"
links:
- postgres:postgres
- redis:redis
environment:
- ...
volumes:
- public:/app/public
- data:/app/data
- uwsgi_spooler:/var/lib/uwsgi
- weblogs:/var/log
command: /usr/local/bin/uwsgi --socket=:8000 ...
nginx:
container_name: feamp_nginx
restart: always
...
postgres:
container_name: feamp_postgres
restart: always
image: postgres:11-alpine
...
redis:
container_name: feamp_redis
restart: always
image: redis:latest
...
volumes:
...
networks:
default:
external:
name: webproxy
Normally I can up the stack from my local laptop and manage it from the CI/CD pipeline on gitlab, or vice-versa.
This diagram should help visualise the situation.
+-----------------+
| |
| Remote server |
| |
+----|--------|---+
| |
| |
docker-compose ps| |docker-compose up -d
| |
| |
+-------------------+ | | +--------------------+
| | | | | |
| Docker client 1 ---------+ +--------- Docker client 2 |
| | | |
+-------------------+ +--------------------+
Connection to the remote server's docker engine are executed through docker-machine.

It appears that specifying the project name when invoking docker-compose commands, solves the issue.
This can be done using the -p parameter in the command line or the COMPOSE_PROJECT_NAME environment variable, on both clients.
For some reasons, this was not needed in previous projects.
It may be a change in docker (I changed from 18 to 19), or something else, I still do not know the details.

Instead of using docker-compose ps you can try docker ps -a and work from there.
Assuming you are ok simply discarding the containers, you can brute-force removal by calling:
docker rm -f 40586885
docker network rm webproxy

In my case, the problem was the project name into .env file
docker compose 3.7, docker latest version (2022-07-17)
COMPOSE_PROJECT_NAME = demo.sitedomain-testing.com
The compose project name do not support dots (.).
I just replaced by hifen and it worked well.
COMPOSE_PROJECT_NAME = demo-sitedomain-testing-com
In addition, container name also, do not support dots.

Related

Is there something missing in docker getting-started tutorial?

I'm going through getting-started tutorial (https://www.docker.com/101-tutorial - Docker Desktop) from docker and they have this docker-compose here:
version: "3.7"
services:
app:
image: node:12-alpine
command: sh -c "yarn install && yarn run dev"
ports:
- 3000:3000
working_dir: /app
volumes:
- ./:/app
environment:
MYSQL_HOST: mysql
MYSQL_USER: root
MYSQL_PASSWORD: secret
MYSQL_DB: todos
mysql:
image: mysql:5.7
volumes:
- todo-mysql-data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: todos
volumes:
todo-mysql-data:
The problem is that MySQL is not creating the "todos" database.
And then my application can't connect to it giving me this error:
app_1 | Error: ER_HOST_NOT_PRIVILEGED: Host '172.26.0.2' is not allowed to connect to this MySQL server
app_1 | at Handshake.Sequence._packetToError (/app/node_modules/mysql/lib/protocol/sequences/Sequence.js:47:14)
app_1 | at Handshake.ErrorPacket (/app/node_modules/mysql/lib/protocol/sequences/Handshake.js:123:18)
app_1 | at Protocol._parsePacket (/app/node_modules/mysql/lib/protocol/Protocol.js:291:23)
app_1 | at Parser._parsePacket (/app/node_modules/mysql/lib/protocol/Parser.js:433:10)
app_1 | at Parser.write (/app/node_modules/mysql/lib/protocol/Parser.js:43:10)
app_1 | at Protocol.write (/app/node_modules/mysql/lib/protocol/Protocol.js:38:16)
app_1 | at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:91:28)
app_1 | at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:525:10)
app_1 | at Socket.emit (events.js:310:20)
app_1 | at addChunk (_stream_readable.js:286:12)
app_1 | --------------------
app_1 | at Protocol._enqueue (/app/node_modules/mysql/lib/protocol/Protocol.js:144:48)
app_1 | at Protocol.handshake (/app/node_modules/mysql/lib/protocol/Protocol.js:51:23)
app_1 | at PoolConnection.connect (/app/node_modules/mysql/lib/Connection.js:119:18)
app_1 | at Pool.getConnection (/app/node_modules/mysql/lib/Pool.js:48:16)
app_1 | at Pool.query (/app/node_modules/mysql/lib/Pool.js:202:8)
app_1 | at /app/src/persistence/mysql.js:35:14
app_1 | at new Promise (<anonymous>)
app_1 | at Object.init (/app/src/persistence/mysql.js:34:12)
app_1 | at processTicksAndRejections (internal/process/task_queues.js:97:5) {
app_1 | code: 'ER_HOST_NOT_PRIVILEGED',
app_1 | errno: 1130,
app_1 | sqlMessage: "Host '172.26.0.2' is not allowed to connect to this MySQL server",
app_1 | sqlState: undefined,
app_1 | fatal: true
app_1 | }
If I run this command alone to spin MySQL, the "todos" database is created:
docker run -d --network todo-app --network-alias mysql -v todo-mysql-data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=secret -e MYSQL_DATABASE=todos mysql:5.7
Is there any command that was updated or that doesn't work properly on windows with docker-compose?
TL;DR;
Run the command
docker-compose down --volumes
to remove any problematic volume created during the tutorial early phases, then, resume your tutorial at the step Running our Application Stack.
I suppose that the tutorial you are following is this one.
If you did follow it piece by piece and tried some docker-compose up -d in the step 1 or 2, then you've probably created a volume without your todos database.
Just going docker-compose down with your existing docker-compose.yml won't suffice because volumes is exactly made for this, the volume is the permanent storage layer of Docker.
By default all files created inside a container are stored on a writable container layer. This means that:
The data doesn’t persist when that container no longer exists, and it can be difficult to get the data out of the container if another process needs it.
A container’s writable layer is tightly coupled to the host machine where the container is running. You can’t easily move the data somewhere else.
Writing into a container’s writable layer requires a storage driver to manage the filesystem. The storage driver provides a union filesystem, using the Linux kernel. This extra abstraction reduces performance as compared to using data volumes, which write directly to the host filesystem.
Docker has two options for containers to store files in the host machine, so that the files are persisted even after the container stops: volumes, and bind mounts. If you’re running Docker on Linux you can also use a tmpfs mount. If you’re running Docker on Windows you can also use a named pipe.
Source: https://docs.docker.com/storage/
In order to remove that volume, you probably created without your database there is an extra flag you can add to docker-compose down: the flag --volumes or, in short -v
-v, --volumes Remove named volumes declared in the `volumes`
section of the Compose file and anonymous volumes
attached to containers.
Source: https://docs.docker.com/compose/reference/down/
So your fix should be as simple as:
docker-compose down --volumes
docker-compose up -d, so back in your tutorial at the step Running our Application Stack
docker-compose logs -f as prompted in the rest of the tutorial
Currently you're database todo is created inside your mysql container when you launch docker-compose start.
In fact, your issue come from mysql user permission.
Add the line below, at the end of your file which initialize todo database
CREATE USER 'newuser'#'%' IDENTIFIED BY 'user_password';
That line will create a user : newuser and give it access from any host (%) with the password user_password
Follow by this line
GRANT ALL PRIVILEGES ON *.* TO 'newuser'#'%';
It'll grant all permissions on every database and every table you have to newuser from any host
Finally, change your mysql environment variable MYSQL_USER and MYSQL_PASSWORD with the new one you just create
version: "3.7"
services:
app:
image: node:12-alpine
command: sh -c "yarn install && yarn run dev"
ports:
- 3000:3000
working_dir: /app
volumes:
- ./:/app
environment:
MYSQL_HOST: mysql
MYSQL_USER: newuser
MYSQL_PASSWORD: user_password
MYSQL_DB: todos
mysql:
image: mysql:5.7
volumes:
- todo-mysql-data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: todos
volumes:
todo-mysql-data:

Gitlab-CI backup lost by restarting Docker desktop

I have a docker desktop installed on my windows pc. In that, I have self-hosted gitlab on one docker container. Today I tried to back up my gitlab by typing the following command:
docker exec -t <my-container-name> gitlab-backup create
After running this command the backup was successful and saw a message that backup is done. I then restarted my docker desktop and I waited for the container to start when the container started I accessed the gitlab interface but I saw a new gitlab instance.
I then type the following command to restore my backup:
docker exec -it <my-container-name> gitlab-backup restore
But saw the message that:
No backups found in /var/opt/gitlab/backups
Please make sure that file name ends with _gitlab_backup.tar
What can be the reason am I doing it the wrong way because I saw these commands on gitlab official website.
I have this in the docker-compose.yml file:
version: "3.6"
services:
web:
image: 'gitlab/gitlab-ce'
container_name: 'gitlab'
restart: always
hostname: 'localhost'
environment:
GITLAB_OMNIBUS_CONFIG: |
external_url 'http://localhost:9090'
gitlab_rails['gitlab_shell_ssh_port'] = 2224
networks:
- gitlab-network
ports:
- '80:80'
- '443:443'
- '9090:9090'
- '2224:22'
volumes:
- '/srv/gitlab/config:/etc/gitlab'
- '/srv/gitlab/logs:/var/log/gitlab'
- '/srv/gitlab/data:/var/opt/gitlab'
networks:
gitlab-network:
name: gitlab-network
I used this command to run the container:
docker-compose up --build --abort-on-container-exit
If you started your container using Volumes, try looking at C:\ProgramData\docker\volume for your backup.
The backup is normally located at: /var/opt/gitlab/backups within the container. So hopefully you mapped /var/opt/gitlab to either a volume or a bind mount.
Did you try supplying the name of the backup file, as for the omnibus install? When I've restored a backup in Docker, I basically use the omnibus instructions, but use docker exec to do it. Here are the commands I've used from my notes.
docker exec -it gitlab gitlab-ctl stop unicorn 
docker exec -it gitlab gitlab-ctl stop sidekiq 
docker exec -it gitlab gitlab-rake gitlab:backup:restore BACKUP=1541603057_2018_11_07_10.3.4
docker exec -it gitlab gitlab-ctl start 
docker exec -it gitlab gitlab-rake gitlab:check SANITIZE=true
It looks like they added a gitlab-backup command at some point, so you can probably use that instead of gitlab-rake.

Environment variable not set in container when using `environment` key

Following these instructions, I tried setting an environment variable with -e TC=3 and in the compose file like so:
services:
balancer:
environment:
- TC=3
But the variable is not set when the container is run.
Does anyone see what I'm doing wrong?
I'm using:
docker-compose 1.23.1, build b02f1306
Docker 18.06.1-ce, build e68fc7a
The way you are setting the environment is correct. With this compose file
version: '2'
services:
test:
environment:
- HELLO=WORLD
image: alpine
command: env
I got this output
$ docker-compose -f test-compose.yml up
Creating network "sandbox_default" with the default driver
Creating sandbox_test_1 ... done
Attaching to sandbox_test_1
test_1 | PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
test_1 | HOSTNAME=e2eb1a0da23e
test_1 | HELLO=WORLD
test_1 | HOME=/root
sandbox_test_1 exited with code 0
If you want to be able to override a variable written in compose file, then you need to use ${var_name} syntax, e.g.
environment:
- HELLO=${hello_value}

What is the URL of my Docker service?

My docker-compose.yml looks like this:
my_app:
build: .
net: host
command: bin/server
ports:
- "3001:3001"
I start the service like this:
docker-compose up
And I can see that it is running:
my_app_1 | * Listening
But I can't figure out how to access it. None of these work:
localhost:3001
docker-machine:3001
127.0.0.1:3001
0.0.0.0:3001
How can I access my Docker service?
docker inspect container_name/hash | grep Address

Linked container IP not in hosts

I'm trying to configure a simple LAMP app.
Here is my Dockerfile
FROM ubuntu
# ...
RUN apt-get update
RUN apt-get -yq install apache2
# ...
WORKDIR /data
And my docker-compose.yml
db:
image: mysql
web:
build: .
ports:
- 80:80
volumes:
- .:/data
links:
- db
command: /data/run.sh
After docker-compose build & up I was expecting to find db added to my /etc/hosts (into the web container), but it's not there.
How can this be explained ? What am I doing wrong ?
Note1: At up time, I see only Attaching to myapp_web_1, shouldn't I see also myapp_db_1 ?
Note2: I'm using boot2docker
Following #Alexandru_Rosianu's comment, I checked
$ docker-compose logs db
error: database is uninitialized and MYSQL_ROOT_PASSWORD not set
Did you forget to add -e MYSQL_ROOT_PASSWORD=... ?
Since I now set the variable MYSQL_ROOT_PASSWORD
$ docker-compose up
Attaching to myapp_db_1, myapp_web_1
db_1 | Running mysql_install_db
db_1 | ...
I can see the whole db log and the db host effectively set in web's /etc/hosts

Resources