In this lecture I will show you How to connect to Docker Container from VSCode.Step by step process to use docker Container in VSCode.
1: Create a docker file
New-Item -ItemType File -Name Dockerfile
2: Build the Docker Image
docker build -t my-postgres .
3: Run the PostgreSQL Container
docker run -d -p 5432:5432 --name pg_container my-postgres
4: Connect to PostgreSQL/Access the Running Container
docker exec -it pg_container psql -U postgres -d demo
5: Stop the Container
docker stop pg_container
#docker #dockercontainer #vscode
Use the official PostgreSQL image
FROM postgres:latest
Set environment variables
ENV POSTGRES_USER=postgres
ENV POSTGRES_PASSWORD=admin
ENV POSTGRES_DB=demo
Expose PostgreSQL port
EXPOSE 5432
Copy initialization scripts (optional)
COPY ./init.sql /docker-entrypoint-initdb.d/
-- Create a sample table
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL
);
-- Insert some data
INSERT INTO users (name) VALUES ('Alice'), ('Bob');