Docker: Uses an image, skipping (docker-compose) - node.js

I am currently trying out this tutorial for node express with mongodb
https://medium.com/#sunnykay/docker-development-workflow-node-express-mongo-4bb3b1f7eb1e
the first part works fine where to build the docker-compose.yml
it works totally fine building it locally so I tried to tag it and push into my dockerhub to learn and try more.
this is originally what's in the yml file followed by the tutorial
version: "2"
services:
web:
build: .
volumes:
- ./:/app
ports:
- "3000:3000"
this works like a charm when I use docker-compose build and docker-compose up
so I tried to push it to my dockerhub and I also tag it as node-test
I then changed the yml file into
version: "2"
services:
web:
image: "et4891/node-test"
volumes:
- ./:/app
ports:
- "3000:3000"
then I removed all images I have previously to make sure this also works...but when I run docker-compose build I see this message error: web uses an image, skipping and nothing happens.
I tried googling the error but nothing much I can find.
Can someone please give me a hand?

I found out, I was being stupid.
I didn't need to run docker-compose build I can just directly run docker-compose up since then it'll pull the images down, the build is just to build locally

in my case below command worked:
docker-compose up --force-recreate
I hope this helps!

Clarification: This message (<service> uses an image, skipping)
is NOT an error. It's informing the user that the service uses Image and it's therefore pre-built, So it's skipped by the build command.
In other words - You don't need build , you need to up the service.
Solution:
run sudo docker-compose up <your-service>
PS: In case you changed some configuration on your docker-compose use --force-recreate flag to apply the changes and creating it again.
sudo docker-compose up --force-recreate <your-service>

My problem was that I wanted to upgrade the image so I tried to use:
docker build --no-cache
docker-compose up --force-recreate
docker-compose up --build
None of which rebuild the image.
What is missing ( from this post ) is:
docker-compose stop
docker-compose rm -f # remove old images
docker-compose pull # download new images
docker-compose up -d

Related

Run Azure pipline with Selenium tests in Docker

I need to create a pipeline in Azure with my autotests using Docker container. I made it successfully on my local machine using the following algorithm:
Create Selenium node with next command:
docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-chrome:4.0.0-beta-1-20210215
Build image using command: docker build -t my_tests .
next
Here is my dockerfile:
FROM maven:onbuild
COPY src /home/bns_bdd_automation/src
COPY pom.xml /home/bns_bdd_automation
COPY .gitignore /home/bns_bdd_automation
CMD mvn -f /home/bns_bdd_automation/pom.xml clean test
Everything works fine, but locally.
In the cloud I faced an issue: I need to RUN Selenium Node at first, and after that build my image.
As I understood from some articles I need to use docker-compose (for run first image), but I don't know how. Can you help me with that?
Well, here is my docker-compose.yml file:
version: "3"
services:
selenium-hub:
image: selenium/hub
container_name: selenium-hub
ports:
- "4444:4444"
chrome:
image: selenium/node-chrome
depends_on:
- selenium-hub
environment:
- HUB_HOST=selenium-hub
- HUB_PORT=4444
bns_bdd_automation:
depends_on:
- selenium-hub
- chrome
build: .
But it works not as I expected. It builds and RUN tests BEFORE hub and chrome was executed. And after that it shows me in terminal:
WARNING: Image for service bns_bdd_automation was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Starting selenium-hub ... done
Starting bns_bdd_automation_chrome_1 ... done
Recreating bns_bdd_automation_bns_bdd_automation_1 ... error
ERROR: for bns_bdd_automation_bns_bdd_automation_1 no such image: sha256:e5cd6f2618fd9ee29d5ebfe610acd48aff7582e91211bf61689f5161fbb5f889: No such image: sha256:e5cd6f2618fd9ee29d5ebfe610acd48aff7582e91211bf61689f5161fbb5f889
ERROR: for bns_bdd_automation no such image: sha256:e5cd6f2618fd9ee29d5ebfe610acd48aff7582e91211bf61689f5161fbb5f889: No such image: sha256:e5cd6f2618fd9ee29d5ebfe610acd48aff7582e91211bf61689f5161fbb5f889
ERROR: The image for the service you're trying to recreate has been removed. If you continue, volume data could be lost. Consider backing up your data before continuing.
Continue with the new image? [yN]

MongoDB and unrecognised option '--enableEncryption'

I have a problem when I run an image mongo with docker-compose.yml. I need to encrypt my data because it is very sensitive. My docker-compose.yml is:
version: '3'
services:
mongo:
image: "mongo"
command: ["mongod","--enableEncryption","--encryptionKeyFile", "/data/db/mongodb-keyfile"]
ports:
- "27017:27017"
volumes:
- $PWD/data:/data/db
I check the mongodb-keyfile exits in data/db, ok no problem, but when I build the file, made and up the image, and te command is:
"docker-entrypoint.sh mongod --enableEncryption --encryptionKeyFile /data/db/mongodb-keyfile"
The status:
About a minute ago Exited (2) About a minute ago
I show the logs and see:
Error parsing command line: unrecognised option '--enableEncryption'
I understand the error, but I don't known how to solve it. I think to make a Dockerfile with the image an ubuntu (linux whatever) and install mongo with the all configurations necessary. Or try to solved it.
Please help me, thx.
According to the documentation, the encryption is available in MongoDB Enterprise only. So you need to have paid subscription to use it.
For the docker image of the enterprise version it says in here that you can build it yourself:
Download the Docker build files for MongoDB Enterprise.
Set MONGODB_VERSION to your major version of choice.
export MONGODB_VERSION=4.0
curl -O --remote-name-all https://raw.githubusercontent.com/docker-library/mongo/master/$MONGODB_VERSION/{Dockerfile,docker-entrypoint.sh}
Build the Docker container.
Use the downloaded build files to create a Docker container image wrapped around MongoDB Enterprise. Set DOCKER_USERNAME to your Docker Hub username.
export DOCKER_USERNAME=username
chmod 755 ./docker-entrypoint.sh
docker build --build-arg MONGO_PACKAGE=mongodb-enterprise --build-arg MONGO_REPO=repo.mongodb.com -t $DOCKER_USERNAME/mongo-enterprise:$MONGODB_VERSION .
Test your image.
The following commands run mongod locally in a Docker container and check the version.
docker run --name mymongo -itd $DOCKER_USERNAME/mongo-enterprise:$MONGODB_VERSION
docker exec -it mymongo /usr/bin/mongo --eval "db.version()"

Docker-compose and node container as not the primary one

I'm new to Docker and I've successfully set up the PHP/Apache/MySQL. But once I try to add the node container (in order to use npm) it always shuts the container down upon composing up. And yes, I understand that I can use node directly without involving docker, but I find it useful for myself.
And as for composer, I want to use volumes in the node container in order to persist node_modules inside of src folder.
I compose it up using docker-compose up -d --build command.
During composing it shows no errors (even node container seems to be successfully built).
If it might help, I can share the log file (it's too big to include it here).
PS. If you find something that can be improved, please let me know.
Thank you in advance!
Dockerfile
FROM php:7.2-apache
RUN apt-get update
RUN a2enmod rewrite
RUN apt-get install zip unzip zlib1g-dev
RUN docker-php-ext-install pdo pdo_mysql mysqli zip
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN composer global require laravel/installer
ENV PATH="~/.composer/vendor/bin:${PATH}"
docker-compose.yml
version: '3'
services:
app:
build:
.
volumes:
- ./src:/var/www/html
depends_on:
- mysql
- nodejs
ports:
- 80:80
mysql:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: qwerty
phpmyadmin:
image: phpmyadmin/phpmyadmin
links:
- mysql:db
ports:
- 8765:80
environment:
MYSQL_ROOT_PASSWORD: qwerty
PMA_HOST: mysql
depends_on:
- mysql
nodejs:
image: node:9.11
volumes:
- ./src:/var/www/html
As this Dockerfile you are using shows, you are not actually runing any application in the node container so as soon as it builds and starts up - it shuts down because it has nothing else to do.
Solution is simple - provide a application that you want to run into the container and run it like:
I've modified a part of your compose file
nodejs:
image: node:9.11
command: node app.js
volumes:
- ./src:/var/www/html
Where app.js is the script in which your app is written, you are free to use your own name.
edit providing a small improvement you asked for
You are not waiting until your database is fully initialized (depends_on is not capable of that), so take a look at one of my previous answers dealing with that problem here

Docker not propagating file changes from host to container

I am aiming to configure docker so that when I modify a file on the host the change is propagated inside the container file system.
You can think of this as hot reloading for server side node code.
The nodemon file watcher should restart the server in response to file changes.
However these file changes on the host volume don't seem to be reflected inside the container when I inspect the container using docker exec pokerspace_express_1 bash and inspect a modified file the changes are not propagated inside the container from the host.
Dockerfile
FROM node:8
MAINTAINER therewillbecode
# Create app directory
WORKDIR src/app
RUN npm install nodemon -g
# Install app dependencies
COPY package.json .
# For npm#5 or later, copy package-lock.json as well
# COPY package.json package-lock.json ./
RUN npm install
CMD [ "npm", "start" ]
docker-compose.yml
version: '2'
services:
express:
build: .
depends_on:
- mongo
environment:
- MONGO_URL=mongo:27017/test
- SERVER_PORT=3000
volumes:
- ./:/src/app
ports:
- '3000:3000'
links:
- mongo
mongo:
image: mongo
ports:
- '27017:27017'
mongo-seed:
build: ./mongo-seed
links:
- mongo
.dockerignore
.git
.gitignore
README.md
docker-compose.yml
How can I ensure that host volume file changes are reflected in the container?
Try something like this in your Dockerfile:
CMD ["nodemon", "-L"]
Some people had a similar issue and were able to resolve it with passing -L (which means “legacy watch”) to nodemon.
References:
https://github.com/remy/nodemon/issues/419
http://fostertheweb.com/2016/02/nodemon-inside-docker-container/#why-isnt-nodemon-reloading
Right, so with Docker we need to re-build the image or figure out some clever solution.
You probably do not want to rebuild the image every time you make a change to your source code.
Let's figure out a clever solution. Let's generalize the Dockerfile a bit to solve your problem and also help others.
So this is the boilerplate Dockerfile:
FROM node:alpine
WORKDIR '/app'
COPY package.json .
RUN npm install
COPY . .
CMD ["npm", "run", "start"]
Remember, during the image building process we are creating a temporary container. When we make the copies we are essentially taking a snapshot of the contents /src and /public. Its a snapshot that is locked in time and by default will not be updated by making changes to the code.
So in order to get these changes to files /src and /public, we need to abandon doing a straight copy, we are going to adjust the docker run command that we use to start up our container.
We are going to make use of a feature called volume.
With Docker volume we setup a placeholder inside our Docker container, so instead of copying over our entire/src directory we can imagine we are going to put a reference to those files and give us access to the files and folders inside of the local machine.
We are setting up a mapping from a folder inside the container to a folder outside a container. The command to use is a bit painful, but once its documented here you can bookmark this answer.
docker run -p 3000:3000 -v /app/node_modules -v $(pwd):/app <image_id>
-v $(pwd):/app used to set up a volume in present working directory. This is a shortcut. So we are saying get the present working directory, get everything inside of it and map it up to our running container. It's long winded I know.
To implement this you will have to first rebuild your docker image by running:
docker build -f Dockerfile.dev .
Then run:
docker run -p 3000:3000 -v $(pwd):/app <image_id>
Then you are going to very quickly get an error message, the react-scripts not found error. You will see that message because I skipped the -v /app/node_modules.
So what's up with that?
The volume command sets up a mapping and when we do, we are saying take everything inside of our present working directory and map it up to our /appfolder, but the issue is there is no /node_modules folder which is where all our dependencies exist.
So the /node_modules folder got overwritten.
So we are essentially pointing to nothing and thats why we need that -v /app/node_modules with no colon because the colon is to map up the folder inside a container to a folder outside the container. Without the colon we are saying want it to be a placeholder, don't map it up against anything.
Now, go ahead and run: docker run -p 3000:3000 -v $(pwd):/app <image_id>
Once done, you can make all the changes you want to your project and see them "hot reload" in your browser. No need to figure out how to implement Nodemon.
So whats happening there is any changes made to your local file system is getting propagated into your container, the server inside your container sees the change and updates.
Now, I know its hard and annoying to remember such a long command, in enters Docker Compose.
We can make use of Docker Compose to dramatically simplify the command we have to run to start up the container.
So to implement that you create a Docker Compose file and inside of it you will include the port setting and the two volumes that you need.
Inside your root project, make a new file called docker-compose.yml.
Inside there you will add this:
version: '3'
services:
web:
build: .
ports:
- "3000:3000"
volumes:
- /app/node_modules
- .:/app
Then run: docker-compose up
Daniel's answer partially worked for me, but the hot reloading still doesn't work. I'm using a Windows host and had to change his docker-compose.yml to
version: '3'
services:
web:
build: .
ports:
- "3000:3000"
volumes:
- /App/node_modules
- .:/App
(I changed the volumes arguments from /app/node_modules to /App/node_modules and from .:/app to .:/App. This enables changes to be passed to the container, however the hot reloading still doesn't work. I have to use docker-compose up --build each time I want to refresh the app.)

Docker-compose hanging on pulling image where Dockerfile doesn't

I'm having an interesting problem... would love any tips, suggestions, or pointers in the right direction. Not sure where to start with this one, really.
Basically, we have a docker-compose.yml and Dockerfile.
Dockerfile:
FROM hypriot/rpi-node:7
# Create app directory
RUN mkdir -p /usr/src/rrp-database
WORKDIR /usr/src/rrp-database
# Install app dependencies
COPY package.json /usr/src/rrp-database
RUN npm install
# Bundle app source
COPY . /usr/src/rrp-database
docker-compose.yml:
mysql:
image: hypriot/rpi-mysql
environment:
- MYSQL_ROOT_PASSWORD=sqltest
- MYSQL_DATABASE=rrplocal
volumes:
- ./data/mysql:/var/lib/mysql
ports:
- 3306:3306
application:
build: .
working_dir: /opt/rrp/src/rrp-database
ports:
- 8080:8080
links:
- mysql
command: bash -c "sleep 15 && node createTables.js && sleep 5 && node provisionDB.js && node server.js"
Most of this you don't need to delve in to, so he's there problem: when I run the setup via docker-compose build, our machine (a RaspPi, hence the hypriot versions) hangs completely when pulling the hypriot/rpi-node image.
$ docker-compose build
mysql uses an image, skipping
Building application
Step 1 : FROM hypriot/rpi-node:7
7: Pulling from hypriot/rpi-node
395823d8c49b: Extracting [====> ] 4.129 MB/45.86 MBBnload complete
298 B/298 BDownload complete
44f82080e2cc: Download complete
a3ed95caeb02: Download complete
f23aeb340745: Download complete
466adec6a1f2: Download complete
281ed5189bce: Download complete
95c0246ab315: Download complete
0a596801c90f: Downloading [=======================> ] 51.89 MB/111.9 MBnload complete
e1613bd476c1: Download complete
It stays like this forever, and hangs the machine. However, when running the Dockerfile alone - docker build -t rrp-database . (which I thought is essentially what the docker-compose is doing anyway...) the image pulls and builds without a hitch.
It's very much worth noting that this was tested on two separate machines, with the exact same result.
I'd really like to use docker-compose, but I'm not sure where to start with this issue. Any thoughts?
Much appreciation to anyone who has some answers for me! Cheers!

Resources